JavaScript 的定时(Timing )事件——setTimeout()与setInterval()

JavaScript 可以在时间间隔内执行,这就是所谓的定时事件( Timing Events)。


 

㈠Timing 事件

⑴window 对象允许以指定的时间间隔执行代码,这些时间间隔称为定时事件。

⑵通过 JavaScript 使用的有两个关键的方法:

①setTimeout(functionmilliseconds)  :在等待指定的毫秒数后执行函数。

②setInterval(functionmilliseconds)   :等同于 setTimeout(),但持续重复执行该函数。

setTimeout() 和 setInterval() 都属于 HTML DOM Window 对象的方法。

 

㈡setTimeout() 方法

语法:window.setTimeout(function, milliseconds);

    window.setTimeout() 方法可以不带 window 前缀来编写。

⑵第一个参数是要执行的函数。

⑶第二个参数指示执行之前的毫秒数。

 

示例:单击按钮。等待 3 秒,然后页面会提示 "Hello":

<!DOCTYPE html>
<html>
<body>

<p>点击“试一试”。等待 3 秒钟,页面将提示“Hello”。</p>

<button onclick="setTimeout(myFunction, 3000);">试一试</button>

<script>
function myFunction() {
  alert('Hello');
}
</script>

</body>
</html>

 

效果图

 

 

 

㈢setTimeout() 停止执行

⑴clearTimeout() 方法停止执行 setTimeout() 中规定的函数。

语法window.clearTimeout(timeoutVariable)

window.clearTimeout() 方法可以不带 window 前缀来写。

⑶clearTimeout() 使用从 setTimeout() 返回的变量:

myVar = setTimeout(function, milliseconds);

clearTimeout(myVar);

 

示例:

<!DOCTYPE html>
<html>
<body>

<p>点击“试一试”。等 3 秒。该页面将提醒“Hello”。</p>

<p>单击“停止”以阻止第一个函数执行。</p>

<p>(在 3 秒钟之前,必须单击“停止”。)</p>

<button onclick="myVar = setTimeout(myFunction, 3000)">试一试</button>

<button onclick="clearTimeout(myVar)">停止</button>

<script>
function myFunction() {
  alert("Hello");
}
</script>
</body>
</html>

 

㈣setInterval() 方法

⑴setInterval() 方法在每个给定的时间间隔重复给定的函数。

语法window.setInterval(function, milliseconds);

   window.setInterval() 方法可以不带 window 前缀来写。

⑶第一个参数是要执行的函数。

⑷第二个参数每个执行之间的时间间隔的长度。

 

示例:每秒执行一次函数 "myTimer"(就像数字手表)。( 一秒有 1000 毫秒)

<!DOCTYPE html>
<html>
<body>

<p>此页面上的脚本启动这个时钟:</p>

<p id="demo"></p>

<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {
  var d = new Date();
  document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>

</body>
</html>

 

效果图

 

 

㈤setInterval() 停止执行

⑴clearInterval() 方法停止 setInterval() 方法中指定的函数的执行。

语法window.clearInterval(timerVariable)

window.clearInterval() 方法可以不带 window 前缀来写。

⑶clearInterval() 方法使用从 setInterval() 返回的变量:

myVar = setInterval(function, milliseconds);
clearInterval(myVar);

 

示例:

<!DOCTYPE html>
<html>
<body>

<p>此页面上的脚本启动这个时钟:</p>

<p id="demo"></p>

<button onclick="clearInterval(myVar)">停止时间</button>

<script>
var myVar = setInterval(myTimer ,1000);
function myTimer() {
  var d = new Date();
  document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>

</body>
</html>

 

㈥示例:由计时时间创建的时钟

<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById('txt').innerHTML =
  h + ":" + m + ":" + s;
  var t = setTimeout(startTime, 500);
}
function checkTime(i) {
  if (i < 10) {i = "0" + i};  // 在数字 < 10 之前加零
  return i;
}
</script>
</head>

<body onload="startTime()">

<div id="txt"></div>

</body>
</html>

 

参考:w3chool:https://www.w3school.com.cn/js/js_timing.asp

posted @ 2019-09-25 20:14  石海莹  阅读(624)  评论(0编辑  收藏  举报