'Why do JavaScript's settimer/setInterval drop when there is an identical timer in the task queue?
I'm studying JS timer and recently I read this page https://johnresig.com/blog/how-javascript-timers-work/
Please note the following in this article.
Note that while mouse click handler is executing the first interval callback executes. As with the timer its handler is queued for later execution. However, note that when the interval is fired again (when the timer handler is executing) this time that handler execution is dropped. If you were to queue up all interval callbacks when a large block of code is executing the result would be a bunch of intervals executing with no delay between them, upon completion. Instead browsers tend to simply wait until no more interval handlers are queued (for the interval in question) before queuing more.
And in Commnets, we can see below:
Abhi (February 25, 2008 at 2:16 pm) Just to make sure I understood let me reiterate. If an interval handler is already queued up for execution and before it gets a chance to execute another interval fires, then the newly fired interval’s handler will be dropped. But if an interval handler is executing and while it is executing another interval fires then the newly fired handler will be executed immediately after the currently executing interval handler is done executing. Sorry if I sound too convoluted.
John Resig (February 25, 2008 at 3:29 pm) @Abhi: Yes, that’s correct.
Just to note that in your second example “But if an interval handler is executing and while it is executing another interval fires then the newly fired handler will be executed immediately after the currently executing interval handler is done executing.” it may not be the absolute next thing to execute (since another timer or event may be already queued) but it’ll certainly occur after no artificial delay.
But, yes, you are correct in your assumption – and, yes, this is quite convoluted.
If my understanding is correct, in JavaScript it seems to be dropped when the same timer is pushed while the timer is on the task queue (not yet running, just waiting). Can anyone explain why this is happening? I checked the HTML spec about timer(https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html), but couldn't find anything like this.
Edit) I want to know is there any condition like browser choose to push or drop the task to task queue
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
