tags:

views:

66

answers:

2

i am trying to clearInterval for 60sec and restart when the 60sec are up jquery

A: 

The clearInterval is just as usual. Then you use a setTimeout to get code to run 60 seconds later, where you start the interval:

window.clearInterval(myTimer);
window.setTimeout(function() {
   myTimer = window.setInterval(timerFunc, intervalMilliseconds);
}, 60000);

Note: This is just plain Javascript and has nothing to do with jQuery.

Guffa
+1  A: 

Ok, so here is a shot in the dark, but lets say you have this code running:

// Will run every 1 second
var myTimer;
function startTimer(){
     myTimer = window.setInterval(function(){
        // Your cool code
     }, 1000); 
};
startTimer();

So, later in your code you could do something like this (I have it arbitrarily attached to a click event, but it could appear anywhere):

$("#pause_timer").click(function(){
    window.clearInterval(myTimer);
    window.setTimeout(function(){
        startTimer();
    }, 60 * 1000); // Wait 60 seconds
});
Doug Neiner
You forgot to assign the timer handle to the myTimer variable in the startTimer function.
Guffa
@Guffa You were typing that while I caught the error and was fixing it. I was like... hmmm... something is missing. Thanks for the heads up!
Doug Neiner
the "function(){ startTimer(); }" can be replaced with "startTimer"
Wallacoloo
@wallacoloo I used this syntax to avoid a memory leak. Was trying to find a link to the MDC article that suggests the pattern I used. You are right though, but I don't know if one works better than the other.
Doug Neiner