BobbyDank
BobbyDank

Reputation: 329

Stop a Jquery/Javascript timer

I know this can't be that difficult, but I swear I can't find a straight forward answer to this. I have the following javascript/jquery function that starts a timer:

function startTimer() { (function ($) {
    //timer for the box
    window.timer = window.setInterval(function() {
       $(".region-brand-window").timer();
    }, 10000);

    jQuery.fn.timer = function() {
       changeBrandOnTimer();
    }
})(jQuery); }   

How do I stop this thing? And I don't mean pause. I mean turn it off from another function.

Upvotes: 0

Views: 581

Answers (4)

Anoop
Anoop

Reputation: 23208

call stopTimer whenever you want to stop timer.

    function startTimer() { (function ($) {
        //timer for the box
        window.timer = window.setInterval(function() {
           $(".region-brand-window").timer();
        }, 10000);

        jQuery.fn.timer = function() {
           changeBrandOnTimer();
        }
    })(jQuery); } 

   function stopTimer(){
           clearInterval(timer );
   }

Upvotes: 1

Kelvin
Kelvin

Reputation: 5287

To cancel an interval, you would use:

clearInterval(window.timer);

FYI, if it was a timeout you would use clearTimeout() in the same way.

Upvotes: 2

epascarello
epascarello

Reputation: 207501

use window.clearInterval(intervalID)

window.clearInterval(window.timer)

Upvotes: 1

j08691
j08691

Reputation: 207891

clearInterval(window.timer);

Should do it.

Upvotes: 2

Related Questions