• 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.  Using JavaScript to Subtract Days From a Date
  • 2.  Using JavaScript to Get the Height of an Element
  • 3.  Using JavaScript to Check if Array is Empty
  • 4.  Using JavaScript to Redirect to a Relative URL
  • 5.  Using JavaScript to Get a Random Number Between 1 and 20
  • 6.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 7.  Using JavaScript to Change Text of Element
  • 8.  How to Remove Non Numbers From String in JavaScript
  • 9.  How to Clear a Textarea Field In JavaScript
  • 10.  How to Empty a String in JavaScript

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

x