setTimeout setTimeout allows us to run a function once after the interval of time.
setTimeout
In short, the method will be invoked only once after a defined delay time.
setInterval setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.
setInterval
In short, the method will be invoked multiple times after a defined delay time.
setImmediate setImmediate will execute a function right after the next code blocks after it or at the end of the current event loop cycle. in this method, the first argument is the function to execute and you can also pass any extra arguments to this function. Example
setImmediate
console.log('before....'); setImmediate((arg) => { console.log(`executing.... ${arg}`); }, 'setImmediate method'); console.log('after 1....'); console.log('after 2.....');
Prints
before.... after 1.... after 2..... executing.... setImmediate method
Note: setTimeout(() => {}, 0) A setTimeout() callback with a 0ms delay is very similar to setImmediate().
setTimeout(() => {}, 0)