我遇到了一个简单的计数器,从10分钟倒计时出现问题。问题是,当我调用函数countdown()并添加10 [分钟]作为值时,它会在达到9分钟而不是2分钟时停止。倒计时计时器的故障
令人困惑的部分是,当我添加另一个值,比如3分钟时,它的工作原理与预期完全相同。我有一个小提琴演示问题here。
而且,这里是我的脚本:
var timeoutHandle;
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("taoTimer");
var current_minutes = mins - 1
seconds--;
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if (seconds > 0) {
timeoutHandle = setTimeout(tick, 1000);
} else {
if (mins > 1) {
jQuery('#taoTimer').addClass('taoIncreaseUrgency');
alert("Warning! You only have 2 minutes left!");
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithorst
setTimeout(function() {
countdown(mins - 1);
}, 1000);
} else if (seconds === 0) {
// Build the container for pop-up CTA
jQuery('#applicationWrapper').append('<div id="taoTimeOut"></div>');
jQuery('<span id="taoClose">close [x]</span>').appendTo('#taoTimeOut');
jQuery('<span id="taoSessionMsg">Your session has timed out. To resume your booking, please click continue below</span>').insertAfter('#taoClose');
jQuery('<button id="taoContBtn">Continue</button>').insertAfter('#taoSessionMsg');
alert("Time is up!");
// Add page overlay, preventing user interaction
// with the rest of the page.
jQuery('<span id="taoPageOverlay"></span>').prependTo('#guestInfo');
// TEMPORARILY REMOVE THE SESSION TIMEOUT FOR VALIDATION THAT REMOVING IT WORKS
jQuery('#sessionTimeoutCounter').remove();
// Click event for users to remove CTA and continue reservation
jQuery('#taoClose, #taoContBtn, #taoPageOverlay').on('click', function() {
jQuery('#taoTimeOut, #taoPageOverlay').remove();
location.reload(); // reload page
});
}
}
}
tick();
}
countdown(3);
我伸出谁创造了这一点,但没有得到响应了该Developer。任何指导,你可以提供表示赞赏。
检查小提琴,它不是在9分钟停止。有什么问题? – Dij