var count = Number(sessionStorage.getItem('count')) || 3600;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer() {
count = count - 1;
sessionStorage.setItem('count', count)
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
var time_str = hours + ":h " + minutes + ":m " + seconds + ":s";
//document.cookie = 'time_str = hours + ":h " + minutes + ":m " + seconds + ":s"; expires=Thu, 26 March 20:47:11 UTC; path=/'
Array.prototype.forEach.call(document.querySelectorAll('.timer'), function (el ) { el.innerHTML = time_str; });
}
Above is my javascript code for countdown timer. This works fine, but it doesn't stop after "0h:0m:0s" time. it start counting in negative.
Here's a modified version of your code that works for me:
var count = 5;
var counter = setInterval(hockeytimer, 1000);
function hockeytimer() {
count = count - 1;
sessionStorage.setItem('count', count);
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
if (hours === 0 && minutes === 0 && seconds === 0 ) {
var table = document.getElementById("hockeyt");
while (table.rows.length > 0 ) {
table.deleteRow();
}
}
var time_str = hours + ":h " + minutes + ":m " + seconds + ":s";
var timerCells = document.querySelectorAll('.hockeytimer');
for (var i = 0; i < timerCells.length; i++) {
var timerCell = timerCells[i];
timerCell.innerHTML = time_str;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.