JavaScript 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 invokedclearTimeout(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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours