setTimeout and setInterval in async JavaScript
Learn Asynchronous Time Manipulation
When talking about synchronous and asynchronous code, setTimeout
and setInterval
are often used to simulate asynchronous execution. This article will detail their underlying principles and actual usage.
setTimeout
If you want to execute a piece of code after a certain period, you would use setTimeout
, which is a method found in the DOM API and achieves the functionality of “execute function y after x milliseconds.”
setTimeout(callbackFunction, waitMilliseconds);
// Example 1: Using a named functionfunction callback() { // ...}setTimeout(callback, 1000);
// Example 2: Using an anonymous functionsetTimeout(() => { // ...}, 1000);
Let’s take a more practical case: Joe learns that dinner is ready but he is on the computer, so he tells his family he will leave the computer to eat dinner in one minute.
// Input// Informing the family to wait one more minuteconsole.log('I will be over in one minute.');
// After one minute, Joe goes to eat dinnersetTimeout(() => { console.log('Joe has gone to eat dinner.');}, 60 * 1000);
// Output'I will be over in one minute.'(after one minute);('Joe has gone to eat dinner.');
setInterval
setTimeout
only executes once, while setInterval
repeatedly executes the given function at the specified interval. They have exactly the same syntax:
setInterval(callbackFunction, waitMilliseconds);
// Inputlet timePassed = 0;
setInterval(() => { timePassed += 1; console.log(timePassed);}, 1000);
// Output123...
Canceling the Countdown
To clear a timer, you can use clearInterval(timerId)
or clearTimeout(timerId)
. Each time you use the above methods, you will receive a “identifier ID” that you can pass to clearTimeout
or clearInterval
to terminate the entire scheduled call.
const timerId = setTimeout(...);clearTimeout(timerId);
const timerId = setInterval(...);clearInterval(timerId);
The Theory Behind
Everything seems so natural, but upon closer inspection, aren’t there some unreasonable things?
JavaScript runs in a single thread in the browser, meaning it can only execute one task at a time. So how can it manage to both “wait” and “execute subsequent code” at the same time?
In reality, JavaScript does execute setTimeout
, but it delegates the “waiting” part to the Web API. For more details, you can refer to another article I’ve written at the bottom: Async JavaScript Visualized With Ease.
Zero Delay
When set the delay for setTimeout
to 0 seconds, it doesn’t mean that the contents of the callback will execute immediately. Indeed, one must consider whether there are still tasks to complete in the Call Stack. This parameter only indicates “the least waiting time requested from the execution environment, not a guaranteed time.”
Therefore, for precise timing, you should avoid using setTimeout
.
// InputsetTimeout(() => { console.log('bar');}, 0);
console.log('foo');
// Output('foo');('bar');
References
- YouTube - What the heck is the event loop anyway? | Philip Roberts | JSConf EU
- PJCHENder - [JS] Understanding the Event Loop, Stack, Queue, and Concurrency Mode of JavaScript in Depth
- MDN - Concurrency Model and Event Loop