Will the setTimeout callback in the job queue run or clear?

If the callback for the call is setTimeoutadded to the job queue (for example, if it is next in the job queue), and clearTimeoutis called in the current tick of the event loop, the idoriginal setTimeoutcall. Will there be a callback setTimeoutin the job queue?

Or does the runtime magically remove the callback from the job queue?

+4
source share
2 answers

No, it will not work; it will be queued and then interrupted. The specification goes through several steps when you call setTimeout, one of which (after the minimum timeout, plus a user agent with missed timeouts, etc.) ultimately:

  1. Queue to task task.

This seems to be happening regardless of whether the handle that was returned in step 10 was cleared, i.e. the call setTimeoutwill always cause something to be in the queue.

When you call clearTimeoutit :

should clear the entry identified as a descriptor from the list of active timers

.. , setTimeout. , , , :

  1. - , :

    • , .

, , , .

+4

, .

, , , , .

function f() {
    var t1 = setTimeout(function() { console.log("YES"); }, 2000);
    sleep(3000);
    clearTimeout(t1);
    console.log("NO");
}
+1

Source: https://habr.com/ru/post/1610590/


All Articles