JavaScript Timing 事件及两种时钟写法

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":

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

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

完整实例:

<!DOCTYPE html>
<html>
<body>

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

<button onclick="setTimeout(myFunction,3000);">试一试</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);

实例

类似上例,但是添加了“停止”按钮:

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

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

完整实例:

<!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"(就像数字手表)。

实例

显示当前时间:

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

完整实例

<!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>

一秒有 1000 毫秒。

--------------------------------------------------------------------------------------------------------------------------------------------

如何停止执行?

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

window.clearInterval(timerVariable)

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

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

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

实例

类似上例,但是我们添加了一个“停止时间”按钮:

<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>

完整实例:

<!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>
<body>

<button onclick="timedText()">试一试</button>

<p id="demo">点击“试一试”。我将在两秒,四秒和六秒过后显示。</p>

<script>
function timedText() {
  setTimeout(myTimeout1,2000) 
  setTimeout(myTimeout2,4000) 
  setTimeout(myTimeout3,6000) 
}
function myTimeout1() {
  document.getElementById("demo").innerHTML = "2 秒";
}
function myTimeout2() {
  document.getElementById("demo").innerHTML = "4 秒";
}
function myTimeout3() {
  document.getElementById("demo").innerHTML = "6 秒";
}
</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>

  

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小