一矩动、setInterval() 方法:
示例:
// 每隔三秒(3000 毫秒)彈出 "Hello"
setInterval(function(){ alert("Hello"); }, 3000);
注意:setInterval() 方法會不停地調用函數(shù),直到 clearInterval() 被調用或窗口被關閉纯命,由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數(shù)争涌;
提示:1000 毫秒= 1 秒;
二音五、setTimeout()方法:
提示: 如果只想執(zhí)行一次可以使用 setTimeout() 方法惫撰;
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="開始計時!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止計時躺涝!" onClick="stopCount()">
</form>
<p>
請點擊上面的“開始計時”按鈕润绎。輸入框會從 0 開始一直進行計時。點擊“停止計時”可停止計時诞挨。
</p>
</body>
</html>
三莉撇、JS倒計時
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js時分秒毫秒倒計時</title>
</head>
<body>
<div>
<span id="_d">00</span>
<span id="_h">00</span>
<span id="_m">00</span>
<span id="_s">00</span>
<span id="_ms">00</span>
</div>
<script type="text/javascript">
function countTime() {
var date = new Date();
var now = date.getTime();
var endDate = new Date("2018-10-01 00:00:00"); //設置截止時間
var end = endDate.getTime();
var leftTime = end - now; //時間差
var d, h, m, s, ms;
if (leftTime >= 0) {
d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
m = Math.floor(leftTime / 1000 / 60 % 60);
s = Math.floor(leftTime / 1000 % 60);
ms = Math.floor(leftTime % 1000);
if (ms < 100) {
ms = "0" + ms;
}
if (s < 10) {
s = "0" + s;
}
if (m < 10) {
m = "0" + m;
}
if (h < 10) {
h = "0" + h;
}
} else {
console.log('已截止')
}
//將倒計時賦值到div中
document.getElementById("_d").innerHTML = d + "天";
document.getElementById("_h").innerHTML = h + "時";
document.getElementById("_m").innerHTML = m + "分";
document.getElementById("_s").innerHTML = s + "秒";
document.getElementById("_ms").innerHTML = ms + "毫秒";
//遞歸每秒調用countTime方法,顯示動態(tài)時間效果
setTimeout(countTime, 50);
}
countTime();
</script>
</body>
</html>
參考:https://blog.csdn.net/x550392236/article/details/78033423