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

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

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

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.