How can I clearInterval() for all setInterval()?

Asked
Active3 hr before
Viewed126 times

8 Answers

clearintervalsetinterval
90%

When you set an interval, you get a pointer to it. To clear all intervals, you'll need to store all of them:,If you want to cancel an interval, you do the following:,My question is: How do I clear all the setIntervals when I no longer need them? I have tried storing the setInterval in a variable and call window.clearInterval(int), but that only clears the last setInterval since each variable is overridden.,When you set an interval, you get a pointer to it:

When you set an interval, you get a pointer to it:

var myInterval = setInterval(function() {}, 4000);

If you want to cancel an interval, you do the following:

clearInterval(myInterval);

So, for what you want to do, you would do the following:

var intervals = [];
$(".elements").each(function() {
   var i = setInterval(function() {

   }, 1000);
   intervals.push(i);
});

Then if you need to cancel them all you can do this:

intervals.forEach(clearInterval);
load more v
88%

Problem Declaring a setInterval() without keeping a reference to it (which is returned from the function call setInterval(), it returns an id number for the registered event).,It you can see it assigns a random number as the id for the event. Then when we clear the interval it uses that id.,A Diirty Solution Now this is not good coding practice (for a number of reasons) but if you really need to clear that interval we can simply do a loop to find the id of the event to stop all setInterval() events that are running. I’ve done a quick jsfiddle to show you. https://jsfiddle.net/WqCws/.,I’m pretty sure this will also work for setTimeout() and clearTimeout();

Like so:

var interval = setInterval(function() {
   console.log('i');
}, 1000);
console.log(interval);
load more v
72%

The JavaScript setInterval() method returns an ID which can be used by the clearInterval() method to stop the interval.,The setInterval() returns a variable called an interval ID. You can then use it to call the clearInterval() function, as it's required by the syntax:,In some cases, you might need to make JavaScript stop setInterval() from being executed before the times comes. You'll need to use the clearInterval() method. It's meant to stop the timer set by using the setInterval JavaScript function.,clearInterval() itself has no return value: the only result is achieves is making JavaScript stop setInterval() from running.

setInterval(() => {
   alert("Hello");
}, 3000);
load more v
65%

As we said before, setTimeout() executes a particular block of code once after a specified time has elapsed. It takes the following parameters:,Just like setTimeout(), setInterval() returns an identifying value you can use later when you need to clear the interval.,There is another way to use setTimeout(): you can call it recursively to run the same code repeatedly, instead of using setInterval().,Introduction to the server-side

let myGreeting = setTimeout(() => {
   alert('Hello, Mr. Universe!');
}, 2000);
load more v
75%

I know that setInterval() returns a unique ID on every loop, and that clearInterval must be called with that ID to kill the loop. I believe my code does this, yet the loop continues to run indefinitely.,I have checked this in debug and clearInterval() is definitely being called and no errors are thrown. Am I doing something obviously wrong?,In my concern, the problem is the request is longer than 3 seconds, and you still receive connections that your previously launched.,You add a second level of sync... Are you sure that this request is faster than 3000ms?

I know that setInterval() returns a unique ID on every loop, and that clearInterval must be called with that ID to kill the loop. I believe my code does this, yet the loop continues to run indefinitely.

var refresh = setInterval(function() {
   $.get(url, function(data) {
      success: {
         if (data < 5) {
            data = 5;
         }
         var width = data + "%";
         $("#recalculation-progress-bar").css('width', width);

         if (data > 99) {
            clearInterval(refresh);
            $("#recalculation-message").text("Recalculation complete.");
         }
      }
   });

}, 3000);
load more v
40%

Search Answer Titles

var myInterval = setInterval(function() {
   console.log("mmk")
}, 2000);

clearInterval(myInterval); //stop that interval
load more v
22%

There is no direct way to do what you are looking for. Instead, you could set timer to false every time you call clearInterval:,Now, timer will either be false or have a value at a given time, so you can simply check with,For instance, I am setting an interval like,it does clear the interval but is there a way to check that it cleared the interval? I’ve tried getting the value of it while it has an interval and when it doesn’t but they both just seem to be numbers.

timer = setInterval(fncName, 1000);
load more v
60%

To stop further calls, we should call clearInterval(timerId).,setTimeout() triggers the expression only once whilesetInterval() keeps triggering expressionregularly after the given interval of time. (unless you tell it to stop).,where expression is the JavaScript code to run after timeout milliseconds have elapsed.,To track the timeout, setTimeout() also returns a numeric timeout ID. This is most commonly used with the clearTimeout() method.

The setTimeout() function is commonly used if you wish to run your function a specified number of milliseconds from when the setTimeout() method was called. The general syntax of the method is:

setTimeout(expression, timeout);
load more v

Other "clearinterval-setinterval" queries related to "How can I clearInterval() for all setInterval()?"