click , window load:
jQuery(function($){
$('#startClock').on('click', doCount);
});
function doCount(){
var counter = 5;
setInterval(function() {
counter--;
if (counter >= 0) {
span = document.getElementById("count");
span.innerHTML = counter;
}
if (counter === 0) {
alert('sorry, out of time');
clearInterval(counter);
}
}, 1000);
}
I also need to suggest that you use $(function(){});the ready function at home instead, window.onload = function(){};just because dom ready did not wait for dom to fully load in order to execute the script.
source
share