2017-08-01 155 views
0

我遇到了一个简单的计数器,从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。任何指导,你可以提供表示赞赏。

+0

检查小提琴,它不是在9分钟停止。有什么问题? – Dij

回答

1

倒计时不是在9:00停止,显示左侧只有2分钟的警报每分钟后出现,那是因为你已经把它if (mins > 1)下解决这个问题,你可以把另一个条件显示警报之前,像这样的东西会解决它:

if (mins > 1) { 
    if (mins === 3){ 
     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) { 
+0

这个伎俩!我仍然不得不做一个小小的调整,即在它工作的时候,我不明白为什么:保持“if(mins === 2)”在1分钟时触发我的console.log。将其更改为“if(mins === 3)”在2分钟时正确执行。 谢谢,Dij! – Brion

+0

这是正确的,它应该适用于(分钟=== 3),原因是第三分钟的秒数变为0,然后您从超时时间调用倒计时(min-1),这意味着在设置超时2分钟之前剩下的时间。 – Dij

0

您的代码旨在在每分钟后警告您。

if (seconds > 0) { 
      timeoutHandle = setTimeout(tick, 1000); 
     } else { 

关于左分钟的消息是固定的,并且总是通知大约2分钟的左向左。

alert("Warning! You only have 2 minutes left!");