我遇到了一個簡單的計數器,從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