简体   繁体   English

如何停止倒数计时器

[英]How to stop count down timer

Am working with a count down timer and I want to stop it when is 0, Here's my code 我正在使用倒数计时器,我想在它为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);
}

You'd clear the interval inside the condition 您将清除条件内的间隔

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);
}

I think you are looking for clearInterval : 我认为您正在寻找clearInterval

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

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM