js 倒计时

基本介绍

以下计时器满足两种业务逻辑,一个是根据页面时间倒计时,一个是指定时间倒计时。

根据页面时间倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(function(){
var startCountDown = function(){
var hours = parseInt($("#hour").text());
var minutes = parseInt($("#minute").text());
var seconds = parseInt($("#second").text());
if (seconds <= 0) {
if (minutes <= 0) {
if (hours <= 0) {
alert("时间结束!");
return ;
} else {
hours--;
}
minutes = 59;
} else {
minutes--;
}
seconds = 59;
} else {
seconds--;
}
$("#second").html((seconds).toString().length > 1 ? seconds : "0" + seconds);
$("#minute").html((minutes).toString().length > 1 ? minutes : "0" + minutes);
$("#hour").html((hours).toString().length > 1 ? hours : "0" + hours);
time = setTimeout(startCountDown, 1000);
};
window.startCountDown = startCountDown;
})();

指定时间倒计时

1
2
3
4
5
6
7
8
9
10
11
12
(function(){
var seconds = 60; // 指定时间
var startCountDown = function(){
if (seconds <= 0) {
console.log("获取超时!");
} else {
console.log("请耐心等待(" + (seconds--) + "s)");
}
time = setTimeout(startCountDown, 1000);
};
window.startCountDown = startCountDown;
})();
------------- 本文结束 感谢您的阅读 -------------

本文标题:js 倒计时

文章作者:水中熊

发布时间:2018年06月12日 - 17:06

最后更新:2018年06月12日 - 17:06

原始链接:https://shuizhongxiong.github.io/js-timeDown.html

许可协议: 知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议进行许可。 转载请保留原文链接及作者。

🌹坚持原创技术分享,您的支持将鼓励我继续创作!🌹
0%