Window.setInterval() (Method)

Schedule a function to be executed at regular intervals.

Availability:

JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Property/method value type:Number primitive
JavaScript syntax:-myWindow.setInterval(aFunction, anInterval)
-myWindow.setInterval(aFunction, anInterval, someArguments)
-myWindow.setInterval(aFunction, anInterval, someArguments, aLanguage)
-myWindow.setInterval(aSourceText, anInterval)
-myWindow.setInterval(aSourceText, anInterval, someArguments)
-myWindow.setInterval(aSourceText, anInterval, someArguments, aLanguage)
-setInterval(aFunction, anInterval)
-setInterval(aFunction, anInterval, someArguments)
-setInterval(aFunction, anInterval, someArguments, aLanguage)
-setInterval(aSourceText, anInterval)
-setInterval(aSourceText, anInterval, someArguments)
-setInterval(aSourceText, anInterval, someArguments, aLanguage)
Argument list:aFunctionA function object
aLanguageA scripting language to execute the script source (MSIE only)
anIntervalA time interval in milliseconds
aSourceTextSome valid script source text
someArgumentsThe arguments to the function object (not supported in MSIE version 4)

The setInterval() method establishes a periodically scheduled execution timer that runs the same fragment of script continuously with a delay timer between each cycle.

If you only want to delay the execution of some code and you want it to be executed just once, then use the setTimeout() method instead. That will defer execution and clear its timer automatically when it executed.

Passing a function as one of the arguments is only supported in MSIE from version 5.0 upwards and Netscape Navigator from version 4 upwards. You can only specify the scripting language in MSIE however.

The simpler form in which a script source text can be passed in a string with a second argument to specify the interval is much more portable and recommended for use. You can pass a multiple line script fragment in this argument as long as each line is separated by a semi-colon. Functionally, this is very similar to the eval() method with an extra repeat periodicity value.

Be careful that you note the value returned by this method if you intend to deactivate the periodic execution. Without that value, you have no way to identify which one of possibly several timers you want to cancel, and take care not to cancel the timer more than once. Cancelling a non-existent periodic timer or deferred action is likely to crash your browser.

The result of this method is an identifying value that can be used with the clearInterval() method to cancel this periodic execution.

Warnings:

See also:eval(), Frame object, Interval handlers, Memory leak, Timeout handlers, Window object, Window.clearInterval(), Window.setTimeout()