SetTimeout()


SetTimeout()

The setTimeout() method is used to schedule a task to be put on the event queue after a given amount of time. The first parameter to setTimeout() is the callback function that is going to be executed. The second parameter is the amount of time to wait before putting the task on the event queue. setTimeout() is non-blocking and other code may run while the setTimeout() task is waiting to be executed.

Notice how setTimeout() is used to schedule a console log after 1000 milliseconds:



clearTimeout()

The clearTimeout() function is used to cancel a timeout that is still pending. The setTimeout() method call returns a numeric timerID that is used to identify the timer. This timerID can be passed into the clearTimeout() method call to stop the timer.

Notice how clearTimeout() is used to stop a setTimeout() callback from executing:

results matching ""

    No results matching ""