如何停止倒计时器

时间:2017-09-17 04:37:08

标签: javascript jquery

我正在使用倒计时器,我想在0时停止它,这是我的代码

 function countdown() {
    // your code goes here
    var count = $('.c').attr('id');
    var timerId = setInterval(function () {
        count--;
         $('.c').text(count)
        if (count == 0) {
            $("#page").load("page.php");
            $('#beforeloading').html('');
            count = 60;
        }
    }, 1000);
}

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您要清除条件

内的间隔
function countdown() {
  var count = $('.c').attr('id');
  var timerId = setInterval(function() {
    count--;
    $('.c').text(count)
    if (count == 0) {
      $("#page").load("page.php");
      $('#beforeloading').html('');

      clearInterval(timerId); // here
    }
  }, 1000);
}

答案 2 :(得分:0)

我认为您正在寻找clearInterval

   if (count == 0) {
        $("#page").load("page.php");
        $('#beforeloading').html('');
        count = 60;
        clearInterval(timerId);
    }

答案 3 :(得分:0)



var Counter=1;
var myInterval= setInterval(function(){
  $("span").text(Counter);
   Counter++;
   if(Counter==11){
     clearInterval(myInterval);
       $("span").append(" Stop");
   }

}, 1000);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Stop After 10s</h4><br>
<span></span>
&#13;
&#13;
&#13;