Timeouts & Intervals
setinterval()
- The
setinterval()function repeatedly runs the same code over and over again at regular intervals.
-
The first parameter is the code to execute.
-
*The second parameter is the duration in milliseconds.
-
After the second parameter, you can pass in zero or more values that represent any parameters you want to pass to the function when it is run.
function greet() {
console.log("Hello");
}
setInterval(greet, 20090)
// -> Logs ‘Hello’ every 2 seconds
clearInterval()
- Intervals keep running a task forever so you should clear the interval when appropriate.
setInterval & setTimeOut: Noteworthy points
- It is possible to achieve the same effect as setinterval with a recursive setTimeout.
setTimeout(function run() {
console.log('Hello')
setTimeout(run, 100);
}, 100);
// output 'Hello' to endless loop each 100ms
-
Duration is guaranteed between executions:
-
Irrespective of how long the code takes to run, the interval will remain the same.
-
Code can take longer to run the the time interval itself? Recursive
setTimeout. -
You can calculate a different delay before running each iteration.
-
The duration interval includes the time taken to execute the code you want to run:
-
The code takes 40ms to run, the interval is 60ms
-
The code takes 50ms to run, the interval is 50ms
-
setinterval is always a fixed interval duration