I am new to jquery so I might be making some. obvious mistake here, but the if statement at the bottom is not running.
I am making a timer, and I want the stopwatch to stop when it reaches 0, although I don't know how to do that either.
var i = 60;
$('button[id=skill]').click(function (e){
$(this).hide(1000)
setInterval(function () {
$("#stopWatch").html(i);
i--;
}, 1000);
});
$("#resetButton").click(function (e) {
i = 60;return false;
});
if(i==0)
{
i=20
//I also want the stopwatch to stop when it reaches 0, don't know how to do that though.
}
You need to save the interval to a variable when you create it. Then you can clear the timer when you are done.
var i = 60;
var timer = null;
$('#skill').click(function() {
$(this).hide(1000);
timer = setInterval(function() {
$("#stopWatch").html(i);
i--;
if (i===0) {
clearInterval(timer);
}
}, 1000);
});
$("#resetButton").click(function() {
i = 60;
return false;
});
EDIT: I've fixed a logic error since my original post. The check for i==0 is now inside the timer where it can actually run. I just copied it from OP's original code without looking close enough. Fixed now!