setInterval()
Similar to setTimeout(), setInterval() repeatedly calls a function with a fixed time delay between each call.
Syntax:
setInterval(function, delay, arg1, arg2, ...);
Example: Printing the each output after interval of 1 second using setTimeout().
let count = 0;
const intervalID = setInterval(() => {
console.log('Function called every 1000 milliseconds');
count++;
if (count === 3) {
clearInterval(intervalID);
}
}, 1000);
Output:
How to Delay a Function Call in JavaScript ?
Delaying a JavaScript function call involves executing a function after a certain amount of time has passed. This is commonly used in scenarios where you want to postpone the execution of a function, such as in animations, event handling, or asynchronous operations.
Below are the methods to delay a JavaScript function call:
Table of Content
- Using setTimeout()
- setInterval()
- Promises with setTimeout()
- Async/Await with setTimeout()
Contact Us