小白教程
所有教程
关于
Search
172.70.174.154
172.70.174.154
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JavaScript 计时事件”的源代码
本页内容
上一节:
JavaScript_弹窗
下一节:
JavaScript_Cookie
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JavaScript 计时事件}}[[Category:Javascript 教程|72]] = JavaScript 计时事件 = {| class="table table-striped table-hover" |width="50%"| 1 2 3 4 5 6 7 8 9 10 11 12 |width="50%"| JavaScript 一个设定的时间间隔之后来执行代码 我们称之为计时事件 |} == JavaScript 计时事件 == 通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。 在 JavaScritp 中使用计时事件是很容易的,两个关键方法是: * setInterval() - 间隔指定的毫秒数不停地执行指定的代码。 * setTimeout() - 在指定的毫秒数后执行指定代码。 '''注意:''' setInterval() 和 setTimeout() 是 HTML DOM Window对象的两个方法。 == setInterval() 方法 == setInterval() 间隔指定的毫秒数不停地执行指定的代码 === 语法 === <pre> window.setInterval("javascript function",milliseconds);</pre> '''window.setInterval()''' 方法可以不使用 window 前缀,直接使用函数 '''setInterval()'''。 setInterval() 第一个参数是函数(function)。 第二个参数间隔的毫秒数 '''注意:''' 1000 毫秒是一秒。 每三秒弹出 "hello" : <sample title="" desc="" lang="html" hererun="1"> setInterval(function(){alert("Hello")},3000); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>单击按钮每3秒出现一个“Hello”警告框。</p> <p>再次点击警告框,经过3秒出现新的警告框,这将一直执行 ...</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ setInterval(function(){alert("Hello")},3000); } </script> </body> </html> </run> 实例展示了如何使用 setInterval() 方法,但是每三秒弹出一次对用户体验并不好。 以下实例将显示当前时间。 setInterval() 方法设置每秒钟执行一次代码,就是手表一样。 显示当前时间 <sample title="" desc="" lang="html" hererun="1"> var myVar=setInterval(function(){myTimer()},1000); function myTimer() { var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>在页面显示一个时钟</p> <p id="demo"></p> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer(){ var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } </script> </body> </html> </run> == 如何停止执行? == clearInterval() 方法用于停止 setInterval() 方法执行的函数代码。 === 语法 === <pre> window.clearInterval(intervalVariable)</pre> '''window.clearInterval()''' 方法可以不使用window前缀,直接使用函数'''clearInterval()'''。 要使用 clearInterval() 方法, 在创建计时方法时你必须使用全局变量: <pre> myVar=setInterval("javascript function",milliseconds);</pre> 然后你可以使用 clearInterval() 方法来停止执行。 以下例子,我们添加了 "停止" 按钮: <sample title="" desc="" lang="html" hererun="1"> <p id="demo"></p> <button onclick="myStopFunction()">停止</button> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer(){ var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } function myStopFunction(){ clearInterval(myVar); } </script> </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>页面上显示时钟:</p> <p id="demo"></p> <button onclick="myStopFunction()">停止</button> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer(){ var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } function myStopFunction(){ clearInterval(myVar); } </script> </body> </html> </run> == setTimeout() 方法 == === 语法 === myVar= window.setTimeout("javascript function", milliseconds); setTimeout() 方法会返回某个值。在上面的语句中,值被储存在名为 myVar 的变量中。假如你希望取消这个 setTimeout(),你可以使用这个变量名来指定它。 setTimeout() 的第一个参数是含有 JavaScript 语句的字符串。这个语句可能诸如 "alert('5 seconds!')",或者对函数的调用,诸如 alertMsg。 第二个参数指示从当前起多少毫秒后执行第一个参数。 提示:1000 毫秒等于一秒。 等待3秒,然后弹出 "Hello": <sample title="" desc="" lang="html" hererun="1"> setTimeout(function(){alert("Hello")},3000); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>点击按钮,在等待 3 秒后弹出 "Hello"。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ setTimeout(function(){alert("Hello")},3000); } </script> </body> </html> </run> == 如何停止执行? == clearTimeout() 方法用于停止执行setTimeout()方法的函数代码。 === 语法 === window.clearTimeout(timeoutVariable) '''window.clearTimeout()''' 方法可以不使用window 前缀。 要使用clearTimeout() 方法, 你必须在创建超时方法中(setTimeout)使用全局变量: myVar=setTimeout("javascript function",milliseconds); 如果函数还未被执行,你可以使用 clearTimeout() 方法来停止执行函数代码。 以下是同一个实例, 但是添加了 "Stop the alert" 按钮: <sample title="" desc="" lang="html" hererun="1"> var myVar; function myFunction() { myVar=setTimeout(function(){alert("Hello")},3000); } function myStopFunction() { clearTimeout(myVar); } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>点击第一个按钮等待3秒后出现"Hello"弹框。</p> <p>点击第二个按钮来阻止第一个函数运行。(你必须在3秒之前点击它)。</p> <button onclick="myFunction()">点我</button> <button onclick="myStopFunction()">停止弹框</button> <script> var myVar; function myFunction(){ myVar=setTimeout(function(){alert("Hello")},3000); } function myStopFunction(){ clearTimeout(myVar); } </script> </body> </html> </run> == 更多实例 == [[try.php?filename=tryjs_timing2|另一个简单的计时]] [[try.php?filename=tryjs_timing_clock|try.php?filename=tryjs_timing_clock]]
返回至“
JavaScript 计时事件
”。
上一节:
JavaScript_弹窗
下一节:
JavaScript_Cookie