xiaoheizhuoer 2014-01-07
在 JavaScritp 中使用计时事件是很容易的,两个关键方法是:
setTimeout()
未来的某时执行代码
clearTimeout()
取消setTimeout()
setTimeout()
语法
代码如下:
var t=setTimeout("javascript语句",毫秒)第二个参数指示从当前起多少毫秒后执行第一个参数。
提示:1000 毫秒等于一秒。
当下面这个例子中的按钮被点击时,一个提示框会在5秒中后弹出。
代码如下:
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!" onClick="timedMsg()">
</form>
</body>
</html>实例 - 无穷循环
要创建一个运行于无穷循环中的计时器,我们需要编写一个函数来调用其自身。在下面的例子中,当按钮被点击后,输入域便从 0 开始计数。
代码如下:
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>
</html>语法
代码如下:
clearTimeout(setTimeout_variable)
实例
下面的例子和上面的无穷循环的例子相似。唯一的不同是,现在我们添加了一个 "Stop Count!" 按钮来停止这个计数器:
代码如下:
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>代码如下:
setInterval() setInterval() - executes a function, over and over again, at specified time intervals
语法:
代码如下:
window.setInterval("javascript function",milliseconds);实例:
代码如下:
<html>
<script type="text/javascript">
setInterval(function() {alert("hello")},500);
</script>
</html>说明:上面例子,执行效果是说每隔500ms就alert("hello");
再来一个时钟:
代码如下:
<html>
<body>
<p id="demo" ></p>
<script type="text/javascript">
setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
</script>
</body>
</html> 如何停止,setInterval()方法??
代码如下:
window.clearInterval()
代码如下:
window.clearInterval(intervalVariable)
代码如下:
The window.clearInterval() method can be written without the window prefix.
To be able to use the clearInterval() method, you must use a global variable when creating the interval method:
myVar=setInterval("javascript function",milliseconds);
Then you will be able to stop the execution by calling the clearInterval() method.实例:
代码如下:
<html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
function stop(){
<html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
function stop(){
clearInterval(temp);
}
</script>
</body>
</html>
}
</script>
</body>
</html>