Window setInterval () method
Definition and Usage
setInterval () method in the specified period (in milliseconds) to call a function or evaluate the expression.
setInterval () method will continue to call the function until the clearInterval () is called or the window is closed. By the setInterval () ID value returned can be used as parameters clearInterval () method.
Tip: 1000 ms = 1 second.
grammar
setInterval(code,millisec,lang)
参数 | 描述 |
---|---|
code | 必需。要调用的函数或要执行的代码串。 |
millisec | 必须。周期性执行或调用 code 之间的时间间隔,以毫秒计。 |
lang | 可选。 JScript | VBScript | JavaScript |
Browser Support
All major browsers support setInterval () method
Examples
Examples
1000 msec per clock () function. Examples also contains a button to stop execution:
<html>
<body>
<input type="text" id="clock">
<script language=javascript>
var int=self.setInterval(function(){clock()},1000);
function clock()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("clock").value=t;
}
</script>
<button onclick="int=window.clearInterval(int)">Stop</button>
</body>
</html>
<body>
<input type="text" id="clock">
<script language=javascript>
var int=self.setInterval(function(){clock()},1000);
function clock()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("clock").value=t;
}
</script>
<button onclick="int=window.clearInterval(int)">Stop</button>
</body>
</html>
try it"
Window object