[英]How to stop count down timer
我正在使用倒數計時器,我想在它為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時clearInterval
://developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval
您將清除條件內的間隔
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);
}
我認為您正在尋找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.