计时器 setTimeout()
执行次数:
setTimeout:只执行一次。setInterval:重复执行,直到被明确停止。用途:setTimeout:适用于一次性的延迟任务。setInterval:适用于需要定期重复执行的任务。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>计时器 setTimeout()</title>
</head>
<body>
<h1 class="time"></h1>
<script>
const time = document.querySelector(".time");
let status = "5秒后执行";
time.innerHTML = status;
setTimeout(() => {
status = "开始执行";
time.innerHTML = status;
}, 5000);
setTimeout(() => {
status = "结束";
time.innerHTML = status;
}, 8000);
</script>
</body>
</html>