Reputation: 340
I use setInterval like so:
game.clock.interval = window.setInterval( game.clock_update, game.clock.delay );
I then later call a function to clear the interval:
game.clock_stop = function() {
clearInterval(game.clock.interval);
}
Edit:
The real issue seems to be when I try to call the method that starts the interval agian. When a certain element (in my case an 'i' tag) is clicked I have it use the clearInterval function above; At the same exact time, I open a Colorbox..., I'm trying to resume the interval when I call onClosed for colorbox.
$("#pause").on("click",function(){
game.pause();
game.clock_stop();
});
game.pause = function() {
$.colorbox({href:"pause.php", title:"The Game Is Paused"});
onClosed:function(){
game.clock_start();
}
};
Upvotes: 0
Views: 120
Reputation: 707416
Edit: based on new info you added to your question.
The code for your game.pause()
method looks wrong. the onClosed:
definition doesn't appear to be part of any object so it's probably not being attached to your colorbox at all.
I'm guessing that it should be this:
game.pause = function() {
$.colorbox({
href:"pause.php",
title:"The Game Is Paused",
onClosed:function(){
game.clock_start();
}
});
};
You just have to call setInterval()
again just like you did the first time:
game.clock.interval = window.setInterval( game.clock_update, game.clock.delay );
This will start a new interval timer based on exactly when you make the new call to setInterval()
.
If you're going to do this in several places, then just like you have a clock_top()
method, you may want to make a clock_start()
method:
game.clock_start = function() {
// if clock not already running, start it
if (!game.clock.interval) {
game.clock.interval = setInterval(game.clock_update, game.clock.delay);
}
}
game.clock_stop = function() {
clearInterval(game.clock.interval);
// clear saved timer so we know the clock is not running any more
game.clock.interval = null;
}
So, anytime you want to start it, you can just call game.clock_start()
.
Upvotes: 3