JavaScript setInterval

Updated on 21 Mar 2012,
Published on 24 Mar 2008

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.

JavaScript Timer Examples:

You can see the live samples and examples of JavaScript Timer Events from the following links:

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

var interval;
   
function startInterval()
{
    interval = setInterval("startTime();",1000);
}

function startTime()
{
    document.getElementById('myTime').innerHTML = Date();   
}

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).

Output:

You can see the output of above discussed code from the following link:

JavaScript setInterval Function

Continue to next tutorial: Javascript clearInterval to learn how to stop timer interval to exit the function execution process.

0 Responses to "JavaScript setInterval"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers