如果时间结束如何提醒

时间:2016-02-25 04:55:45

标签: javascript

 <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)并且它没有显示任何警报消息并且我想在结束时自动提交表单,有人可以帮我吗

1 个答案:

答案 0 :(得分:2)

  

您需要另一个else条件。

试试这个:

&#13;
&#13;
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);;
&#13;
<div id="timer"></div>
&#13;
&#13;
&#13;