![]() ![]() In the timers.js file, change the delay for our timer to be 10 milliseconds - so short, it's almost immediate - and run your code. So the movie will not be able to start at 2:20, even though the time was essentially up. They cannot start the movie until the ad reel is finished because sponsors paid for the ads to be seen. It means the movie assistant needs to start the ad reel at 2:20 sharp - but today they weren't feeling that well and didn't get the ad reel started until 2:25. Let's say the movie is supposed to start at 2:30 PM, and the length of our ad reel is 10 mins. The catch here is to understand that the delay passed into setTimeout() is a minimum delay and it may take more time before the callback is executed. Thus it moved on to the "close events" phase and executed process.exit() finalizing the runtime of our application. Then it started the next tick with the timers phase and executed the callback.Īfter that, it continued through the Event Loop phases until it hit the setImmediate() phase again, realized that there is no more code to be executed, and nothing in the timers queue. The Event Loop went to sleep and was woken up when the timer's delay was up. Since we didn't have any other code after the setTimeout(), but we're still waiting on the timer that's currently in the queue, the code went idle on the setImmediate() callbacks phase. Then it hit the call to setTimeout() and pushed its callback to the timers queue and proceeded to the next phases of the Event Loop. In the "polling" phase Event Loop executed our first console.log() with the ad reel text. The Event Loop moved on to further phases: "I/O callbacks", "idle", and finally, "polling". At the beginning we didn't have any our setTimeout() is further down the code pipe line. It went to the first phase - "timers" - where it checks for timer callbacks. Our application process started and entered the Event Loop. Let's look at what happened here from the perspective of the Event Loop. Then after the delay we see that our movie attendant got the signal and the movie is about to start: $ node timers.js In the console at first we see our pre-show demo reel text: $ node timers.js Once ready, run your code with the command: $ node timers.js setTimeout() function with anonymous callback This is what we see in the console first.Ĭonsole.log("This is a pre-show ad reel. ![]() Your code may look something like this: const alert = "We are ready start the movie" ![]() Add a timer with the delay set to the length of the pre-show. In this file define the variable alert that will tell the movie attendant to start the movie when the pre-show waiting period is up. You are excited, but you're stuck watching the pre-show as the movie attendant is waiting for the cue to run the movie. Imagine that you are at the movie theatre, and are about to watch a movie. delay: The number of milliseconds to delay before calling callbackĪny additional arguments will be passed to the callback function when it executes.callback: The callback function that needs to be called when the delay time is up.The setTimeout() function takes two main arguments: SetTimeout() is a one-time timer that allows us to schedule code to be run after a certain period of time.Įxample: setTimeout(function, delay,,. Scheduling a one time callback with setTimeout() Learn how to use setTimeout() and setInterval() in your code and observe execution of their callbacks during the Event Loop execution. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |