clearTimeout()

Published Jun 20, 2023
Contribute to Docs

clearTimeout() is a window method that stops a previously scheduled setTimeout() from running.

Syntax

clearTimeout(timeoutID);

The clearTimeout() function receives a timeout ID as its argument, which is returned by the corresponding call of setTimeout(), and cancels its execution.

Example

Below is a example of how clearTimeout() works.

const timeoutID = setTimeout(() => {
console.log('This will be canceled and it will not run.');
}, 3000);
// clearTimeout() will cancel the timeout when invoked
clearTimeout(timeoutID);

In the example above, a setTimeout() is invoked allowing to schedule the execution of that function after the 3-second delay (3000ms). However, to cancel that schedule timeout before it runs, the clearTimeout() is called, passing the timeout ID.

All contributors

Looking to contribute?

Learn JavaScript on Codecademy