JavaScript setInterval works similar to setTimeout function in JavaScript. setInterval method calls the specified expression after the specified interval of time in milliseconds.
In the previous articles we discussed about the use of setTimeout and clearTimeout methods and done some examples. Now we will learn the syntax and use of setInterval function of javascript:
JavaScript setInterval syntax:
setInterval( expression, msec, lang);
setInterval function takes 3 parameters:
Expression: Name of the function to call or the javascript expression to evaluate.
Msec: Time in milliseconds after which the specified expression is evaluated.
Lang: Optional parameter to specify the scripting language e.g.: Jscript, Javascript, vbscript.
JavaScript setInterval Example:
HTML code:
<div id="myTime"></div>
<input type="button" value="start Interval" onclick="startInterval();"/>
JavaScript code:
<script language="javascript">
var interval; function startInterval()
{
interval = setInterval("startTime();",1000);
}
function startTime()
document.getElementById('myTime').innerHTML = Date();
</script>
In the above JavaScript setInterval example, there are two functions startInterval that is called upon when a user clicks on start interval button. startInterval function has setInterval javascript method that calls the other function startTime after regular interval of time i.e. 1000 milliseconds (1 second).
Be the first to rate this post
Tags: javascript, javascript setinterval, javascript use, javascript tutorials, learn javascript, javascript examples
10/11/2008 3:40:47 AM