jQuery show setTimeout timer [英] jQuery show setTimeout timer
本文介绍了jQuery show setTimeout timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试构建一个简单的倒计时应用程序。是否可以在setTimeout上显示计时器值,或者我是否必须使用for循环?
I'm trying to build a simple countdown application. Is it possible to show the timer value on setTimeout, or would I have to use a for loop?
谢谢!
推荐答案
setTimeout
:
var n = 100;
setTimeout(countDown,1000);
function countDown(){
n--;
if(n > 0){
setTimeout(countDown,1000);
}
console.log(n);
}
或使用 setInterval
:
var n = 100;
var tm = setInterval(countDown,1000);
function countDown(){
n--;
if(n == 0){
clearInterval(tm);
}
console.log(n);
}
这篇关于jQuery show setTimeout timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文