I want that when count = 0, alert "yes" and when count != 0 alert "no". All work find, until the counter is 0 that alert "yes" works find, but alert "no" jumps too. Why is that? I wish that anyone be able to help me with correct code, thanks and sorry for my english
var i=5;
function countDown(i, callback) {
callback = callback || function(){};
var int = setInterval(function() {
document.getElementById("displayDiv").innerHTML = i;
i-- || (clearInterval(int), callback());
}, 1000);
}
countDown(i, function(){
if($("button").click(function(){
alert('yes');
}));
});
if (i > 1){
if($("button").click(function(){
alert('No');
}));
};
return;
The code here
You need to check the contdown within your click() handlers. At this moment you are assigning the "alert('yes')" handler when the counter reaches 0, but the "alert('no')" handler is also assigned, and never removed.
How's this:
$(function() {
var i=5;
var int = setInterval(function() {
document.getElementById("displayDiv").innerHTML = i;
if(i === 0){
clearInterval(int)
}
i--;
}, 1000);
$("button").click(function(){
var text = i > 0 ? 'no' : 'yes';
alert(text);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="displayDiv"></span>
<button>Go</button>