Thursday, 15 August 2013

Why does decrementation becomes more than -1 at the 2nd time of setInterval?

Why does decrementation becomes more than -1 at the 2nd time of setInterval?

$(document).ready(function(e) {
var count=10;
setInterval(timer, 1000);
var green = true;
function timer()
{
if (count < 1)
{
count = 10;
setInterval(timer, 1000);
green ? green=false : green = true;
}
$('#tl').empty();
if(green)
$('#tl').append(count).css('color', '#090');
else
$('#tl').append(count).css('color', '#F00');
count = count - 1;
}
});
This is a simple traffic light counter. At first it runs as 10 9 8 7 6 5 4
3 2 1, the next time it become 10 8 6 4 2 0, then 10 7 4 1

No comments:

Post a Comment