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
  • 331,213
  • 40
  • 305
  • 339
Awais Mushtaq
  • 633
  • 1
  • 10
  • 23

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
  • 331,213
  • 40
  • 305
  • 339
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,930
  • 9
  • 33
  • 56
Pippi
  • 313
  • 1
  • 4
  • 18