0% found this document useful (0 votes)
6 views3 pages

Code Flow

The document explains the differences between setTimeout(), setInterval(), and setImmediate() in JavaScript. It describes how each function schedules callbacks in the event loop, with setTimeout executing after a specified delay, setInterval repeatedly executing at intervals, and setImmediate running after I/O events. The provided sample code illustrates the control flow and output of these functions in action.

Uploaded by

sajalshri21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Code Flow

The document explains the differences between setTimeout(), setInterval(), and setImmediate() in JavaScript. It describes how each function schedules callbacks in the event loop, with setTimeout executing after a specified delay, setInterval repeatedly executing at intervals, and setImmediate running after I/O events. The provided sample code illustrates the control flow and output of these functions in action.

Uploaded by

sajalshri21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 !!

You might also like