<script type="text/javascript">
function countdown(minutes) {
    var seconds = 60;
    var mins = minutes
    function tick() {
        var counter = document.getElementById("timer");
        var current_minutes = mins-1
        seconds--;
        counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
        if( seconds > 0 ) {
            setTimeout(tick, 1000);

        } else {

            if(mins > 1){

               // countdown(mins-1);   never reach “00″ issue solved:Contributed by Victor Streithors
               setTimeout(function () { countdown(mins - 1); }, 1000);
              alert("time has ended");
              //here i have to auto submit the form when time ends
            }
        }
    }
    tick();
}

countdown(10);

</script>


当时间结束并且计时器应该停止运行时如何显示警报我尝试了以下代码,但是计时器停止为(00.00),并且它不显示任何警报消息,如果要结束,我想自动提交表单,任何人都可以请帮帮我

最佳答案

您将需要另一个else条件。


尝试这个:



function countdown(minutes) {
  var seconds = 60;
  var mins = minutes

  function tick() {
    var counter = document.getElementById("timer");
    var current_minutes = mins - 1
    seconds--;
    counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
    if (seconds > 0) {
      setTimeout(tick, 1000);
    } else {
      if (mins > 1) {
        setTimeout(function() {
          countdown(mins - 1);
        }, 1000);
      } else {
        alert("time has ended");
      }
    }
  }
  tick();
}

countdown(2);;

<div id="timer"></div>

关于javascript - 如何提醒时间是否结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35618668/

10-16 13:36