• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works

cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works

September 4, 2022 Leave a Comment

The clearTimeout() method in JavaScript clears the timer which has been set by a setTimeout() method.

var timer = setTimeout(function(){
  //Code that will after 1000 milliseconds
}, 1000);
    
function clearTimer(){
  clearTimeout(timer);
}

In the code above, the setTimeout() method will run the function after 1000 milliseconds(1 second). We can change that parameter to be however long we want to execute the function call.

You can see that we have another function called clearTimer(). We can stop the setTimeout() method from running the function by using the clearTimeout() method call. We can do this by setting a variable to the setTimeout() method, var timer = setTimeout(), as seen above, and then clearing it by using the clearTimeout() method in JavaScript, clearTimeout(timer);.

We find the best way to learn how to use the clearTimeout() method is to see an example of it. So let’s take a look at one below.


To see how the clearTimeout() method in JavaScript works, let’s create a simple example. Here is code that will run a simple timer.

Let’s start with the simple HTML setup:

<div id="div1">
  <div id="click-me" onclick="startTimer()">Start Timer</div>
  <div id="click-me" onclick="stopTimer()">Stop Stop</div>
</div>

Below will be the JavaScript code to make this very simple timer work.

var timer;
function startTimer(){
  timer = setTimeout(function(){
    alert('5 seconds are up');
  }, 5000);
};

The basic overview of this code is that the setTimeout() method will run a function after 5 seconds have passed. And when that happens, an alert box will be fired stating that “5 seconds are up”.

We also have a button to stop the timer. When this button is pressed, our timer will be stopped and cleared. We will do this using the clearTimeout() method.

Here are the functions for our simple timer.

var timer;
function startTimer(){
  timer = setTimeout(function(){
    alert('5 seconds are up');
  }, 5000);
};

function stopTimer(){
  clearTimeout(timer);
  alert('Timer has been stopped');
};

Here is the code for you to try this simple timer out for yourself.

Code Output:

Start Timer
Stop Stop

Full Code:

<div id="div1">
  <div id="click-me" onclick="startTimer()">Start Timer</div>
  <div id="click-me" onclick="stopTimer()">Stop Stop</div>
</div>

<script>

var timer;
function startTimer(){
  timer = setTimeout(function(){
    alert('5 seconds are up');
  }, 5000);
};

function stopTimer(){
  clearTimeout(timer);
  alert('Timer has been stopped');
};

</script>

Hopefully this article has been useful for you to understand how the clearTimeout() method in JavaScript works.

Other Articles You'll Also Like:

  • 1.  How to Select All Checkboxes in JavaScript
  • 2.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 3.  JavaScript Power Operator
  • 4.  Creating a JavaScript Function to Add Two Numbers
  • 5.  Using JavaScript to get Textarea Value
  • 6.  Using JavaScript to Check if Variable Exists
  • 7.  Using JavaScript to Create an Empty Array
  • 8.  Convert String to Array in JavaScript
  • 9.  JavaScript Change Text Color of a Paragraph
  • 10.  Using JavaScript to Count Even Numbers in an Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy