SetTimeout() , SetInterval() & SetImmediate().
Code:-
// ----- Sample Code to show the difference btw , setTimeout() ,setInterval & setImmediate()-------
Output:-
Description:-
Control flow of the program:-
console.log(“Hello”)
|
setTimeout Function
schedules a callback to run after a 5 Seconds time, the myfunc1 is registered in the
timers phase of the event loop.
|
setImmediate Function
schedules a callback to run at check phase of the event loop after IO events' callbacks.
|
setInterval Function
schedules a callback to run at checker phase of the event loop after IO events'
callbacks.
|
setTimeout Function
After setInterval has been running the next statement is setTimeout which will be added
at the end of the call back queue having the time delay of 13 Seconds , Hence “hello to
the setInterval !” is printed two times as its delay is 6 seconds , thereafter it’s stops
since, the setInterval has been cleared by the clearInterval.
|
Console.log(“Welcome to the demo Session”)
So the output in the end looks like:-
Hello
Welcome to Demo session !!...
Hello for setImmediate !!
Hello for settimeout !!
Hello for setInterval !!
Hello for setInterval !!