我正在使用倒计时器,我想在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);
}
答案 0 :(得分:0)
当计数为0时,您只需要clearInterval
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval
答案 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;