3

How can i stop a interval in javascript?

why does the interval not stop?

setInterval(alarm, 500);
window.clearInterval(alarm);

also tried:

window.setInterval(alarm, 500);
window.clearInterval(alarm);

always the same problem :(

still doesn't work:

var switc = 1;
getValue();
function getValue (){
if(switc == 1){
    var myTimer = window.setInterval(alarm, 500);
}
else if(switc == 0){
    window.clearInterval(myTimer);
}
}
function alarm(){
    console.log("test");
}
Robin Timman
  • 489
  • 2
  • 7
  • 17

4 Answers4

15

When you call setInterval it returns a integer that you use to cancel the event. Store that into a variable and use that variable to cancel the event.

var myTimer = window.setInterval(alarm, 500);
window.clearInterval(myTimer);

EDIT:

Your code does not work since myTimer is a local variable and is reset every single time you call the function!

Make it global.

var myTimer = null;
function getValue (){
    if(switc == 1){
        myTimer = window.setInterval(alarm, 500);
    }
    ...

MDN Docs: window.setInterval

Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.

Syntax

var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);

where

  • intervalID is a unique interval ID you can pass to clearInterval().
  • func is the function you want to be called repeatedly.
  • code in the alternate syntax, is a string of code you want to be executed repeatedly (using this syntax is not recommended for the same reasons as using eval())
  • delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced. Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. If you want to enable this functionality on that browser you must use a compatibility code (see the Callback arguments paragraph).
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

That code evinces a misunderstanding of the API. The setInterval() function takes two arguments: a function to call, and a number representing the number of milliseconds between calls. The function returns a token (a number) that identifies that particular interval timer. The token can be passed to clearInterval() to cancel the timer.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You are tring to clear an non existent interval. Assign the id returned by setInterval() to an variable and use it in clearInterval().

In your case alarm is the function that executes and its not the intervals id

var interval = setInterval(alarm, 500);
clearInterval(interval);
hop
  • 2,518
  • 11
  • 40
  • 56
1
var timer = setInterval(alarm, 500);
Window.clearInterval(timer);

function alarm() {
    // Do stuff
}

You need to save the handle of the interval to a variable so you can reference it later when you want to clear/stop it.

DaveB
  • 9,470
  • 4
  • 39
  • 66