JavaScript 可以按时间间隔执行。
这称为定时事件。
1
2
3
4
5
6
7
8
9
10
11
12
|
window
对象允许以指定的时间间隔执行代码。
这些时间间隔称为定时事件。
使用 JavaScript 的两个关键方法是:
setTimeout(function, milliseconds
)
等待指定的毫秒数后执行函数。
setInterval(function, milliseconds
)
与setTimeout()相同,但重复执行 的功能连续。
setTimeout()
和 setInterval()
都是 HTML DOM Window 对象的方法。
window.setTimeout(function, milliseconds);
window.setTimeout()
方法可以不带 window 前缀。
第一个参数是要执行的函数。
第二个参数表示执行前的毫秒数。
单击一个按钮。等待3秒,页面会提示“Hello”:
<button onclick="setTimeout(myFunction, 3000)">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>Click "Try it". Wait 3 seconds, and the page will alert "Hello".</p>
<button onclick="setTimeout(myFunction, 3000);">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
</body>
</html>
clearTimeout()
方法停止函数的执行 在 setTimeout() 中指定。
window.clearTimeout(timeoutVariable)
window.clearTimeout()
方法可以不带 window 前缀编写。
clearTimeout()
方法使用变量 从 setTimeout()
返回:
myVar = setTimeout(function, milliseconds);
clearTimeout(myVar);
如果该函数尚未执行,您可以通过调用 clearTimeout()
来停止执行 方法:
与上面的示例相同,但添加了“停止”按钮:
<button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button>
<button onclick="clearTimeout(myVar)">Stop it</button>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>Click "Try it". Wait 3 seconds. The page will alert "Hello".</p>
<p>Click "Stop" to prevent the first function to execute.</p>
<p>(You must click "Stop" before the 3 seconds are up.)</p>
<button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button>
<button onclick="clearTimeout(myVar)">Stop it</button>
<script>
function myFunction() {
alert("Hello");
}
</script>
</body>
</html>
setInterval()
方法在每个给定的时间重复给定的函数 时间间隔。
window.setInterval(function, milliseconds);
window.setInterval()
方法可以不带 window 前缀。
第一个参数是要执行的函数。
第二个参数表示每次之间的时间间隔长度 执行。
此示例每秒执行一次名为“myTimer”的函数(就像数字 手表)。
显示当前时间:
setInterval(myTimer, 1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<script>
setInterval(myTimer, 1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
</body>
</html>
一秒有 1000 毫秒。
clearInterval()
方法停止函数的执行 在 setInterval() 方法中指定。
window.clearInterval(timerVariable)
window.clearInterval()
方法可以不带 window 前缀。
clearInterval()
方法使用从 setInterval()
返回的变量:
let myVar = setInterval(function, milliseconds);
clearInterval(myVar);
与上面的示例相同,但我们添加了一个“停止时间”按钮:
<p id="demo"></p>
<button onclick="clearInterval(myVar)">Stop time</button>
<script>
let myVar = setInterval(myTimer, 1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Timing</h2>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<button onclick="clearInterval(myVar)">Stop time</button>
<script>
let myVar = setInterval(myTimer ,1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
</body>
</html>
另一个简单的计时
<!DOCTYPE html>
<html>
<body>
<h2>JavaSript setTimeout()</h2>
<p id="demo">I will display when two, four, and six seconds have passed.</p>
<script>
setTimeout(myTimeout1, 2000)
setTimeout(myTimeout2, 4000)
setTimeout(myTimeout3, 6000)
function myTimeout1() {
document.getElementById("demo").innerHTML = "2 seconds";
}
function myTimeout2() {
document.getElementById("demo").innerHTML = "4 seconds";
}
function myTimeout3() {
document.getElementById("demo").innerHTML = "6 seconds";
}
</script>
</body>
</html>
使用计时事件创建的时钟
<!DOCTYPE html>
<html>
<body onload="startTime()">
<h2>JavaScript Clock</h2>
<div id="txt"></div>
<script>
function startTime() {
const today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
setTimeout(startTime, 1000);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</body>
</html>