var n = 60;
setTimeout(countDown, 1000);
function countDown(){
n--;
if (n > 0){
setTimeout(countDown, 1000);
}
//alert(n);
document.getElementById("div_timer").innerHTML = n;
}
this is my code for count down timer. when clicking the submit button, i need to stop the counter running and should return the counter value. please help...
Instead of using setTimeout, use setInterval and assign it to a variable, so you can clear it with clearInterval by passing the variable you therefore defined as argument.
For example :
var n = 10;
var f = setInterval(function() {
console.log("foo");
n--;
if (n<=0) {
clearInterval(f)
}
}, 1000);
Then in your button click handler, just do a
clearInterval(f)
and you'll get the right n value.
Also note that the first step of the interval is occuring after 1s in my example, which technically makes n reaches 0 after 11s, not 10.
For the following, as your title is a bit weird (javascript php ?), after you get the n value, do an Ajax request to some php-server with n value as a parameter. But I think this should belong to another question.
Br.