JavaScript Timing Events
javaScript Timing Events: javaScript时间事件
有两个方法:
1. setTimeout() - executes a code some time in the future 执行未来要执行的事件,有参数的,一个是方法,一个是毫秒
例:var t=setTimeout("javascript statement",milliseconds);
2. clearTimeout() - cancels the setTimeout() 删除setTimeout()方法.
参数是一个setTimeout()的引用
例:clearTimeout(setTimeout_variable)
例子(1):
<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>
例子(2)
<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>
例子(3)
<html>
<head>
<script type="text/javascript">
var c=0
var tfunction 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>