因此,当用户按下按钮时,此js函数就会运行。但是,只要该用户刷新页面,此倒计时就会消失。因此,无论用户关闭页面还是退出网站,无论发生什么情况,这种倒计时都必须继续。
某些主题说我应该使用浏览器的本地存储,但是如果用户尝试使用其他浏览器或另一台计算机登录会怎样?
那你怎么看?
js:
<script>
var upgradeTime = 600;
var seconds = upgradeTime;
function timer() {
var days = Math.floor(seconds/24/60/60);
var hoursLeft = Math.floor((seconds) - (days*86400));
var hours = Math.floor(hoursLeft/3600);
var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
var minutes = Math.floor(minutesLeft/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = days + ":" + hours + ":" + minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Tamamlandı.";
} else {
seconds--;
}
}
var countdownTimer = setInterval('timer()', 1000);
</script>
html:
<input id="clickMe" type="button" value="clickme" onclick="timer();" />