0

This is how I started toggling classes:

$(document).ready(function() {
    window.setInterval(function() {
        $('.blinkClass').toggleClass('blink');
    }, 500);  
});

How can I stop toggling these classes?

Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
Awais Mushtaq
  • 482
  • 1
  • 7
  • 21

2 Answers2

1

You need to store the timer returned from setInterval to a variable which you can then use in a call to clearInterval():

var timer = window.setInterval(function() {
    $('.blinkClass').toggleClass('blink');
}, 500);  

// later on, in a code block within scope of the above variable... 
clearInterval(timer);
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
0

You can do this :

$(document).ready(function() {
    var myVar  = window.setInterval(function() {
        $('.blinkClass').toggleClass('blink');
    }, 500); 

    //when you want     
    myStopFunction(myVar)
});

function myStopFunction(par) {
    clearInterval(par);
}

See more info at

setInterval

clearinterval

ozil
  • 6,357
  • 8
  • 29
  • 53
Pippi
  • 303
  • 1
  • 3
  • 17