In this post, we will see how to execute function with specified time .
setInterval:
setInterval
is used to run a function repeatedly at specified intervals.- It takes two arguments: the first argument is the function to be executed,
- the second argument is the time in milliseconds to wait between each execution.
const x = setInterval(function() { console.log("Hello World!"); }, 5000);
or const x = setInterval(()=>{ console.log("Hellow Worlds");},5000);
In this example, the code inside the setInterval
function will run every 5 seconds, continuously until the clearInterval
method is called to stop it like shown in below:
// To clear interval
clearInterval(x);
So, in summary, setTimeout
is used to run a function once after a specified amount of time, while setInterval
is used to run a function repeatedly at specified intervals.
Thanks for Reading..