The setInterval() method calls a function or evaluates an expression at a specified interval (in milliseconds).
The setInterval() method keeps calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.
grammar
setInterval(code,millisec[,"lang"])
parameter | describe |
---|---|
code | Required. A function to call or a string of code to execute. |
millisec | must. The time interval, in milliseconds, between periodic executions or invocations of code. |
return value
A value that can be passed to Window.clearInterval() to cancel periodic execution of the code.
Example 1
<html> <body> <input type="text" id="clock" size="35" /> <script language=javascript> var int =self.setInterval("clock()",50)
function clock() { var t=new Date() document.getElementById("clock").value=t } </script> </form> <button onclick="int=window.clearInterval(int)">
Stop interval</button> </body> </html>
Example 2
var minites_flag = query.getminites();
doQuery(minites_flag);
$("#showTime").text((new Date()).format("hh:mm"));
setInterval(function(){doQuery(minites_flag )}, 5*60*1000);//Query every 5 minutes
setInterval(function(){$("#showTime").text((new Date()).format("hh:mm")); }, 60*1000);//Display current time refresh every minute