I want to trigger a setInterval() based on a success of a particular function.
function myCountComplete(){
   //do something
   count++;
   if(count > 10){
      var myVar = setInterval(function(){ setColor() }, 300);
   }
}
function setColor() {
    var x = document.body;
    x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
How can I clear the myVar interval when a button is clicked?
$("#stop").click(function(){
    clearInterval(myVar);
});
                You can always set it on the global scope, i.e.
window.myTimer = setInterval(function(){ setColor() }, 300);
$("#stop").click(function(){
    clearInterval(window.myTimer);
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With