Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Methods
 setInterval Method

  Switch on low bandwidth view
setInterval Method

Evaluates an expression each time a specified number of milliseconds has elapsed.

Syntax

iTimerID = object.setInterval(vCode, iMilliSeconds [, sLanguage])

Parameters

vCode Required. Variant that specifies a function pointer or string that indicates the code to be executed when the specified interval has elapsed.
iMilliSeconds Required. Integer that specifies the number of milliseconds.
sLanguage Optional. String that specifies any one of the possible values for the LANGUAGE attribute.

Return Value

Integer. Returns an identifier that cancels the timer with the clearInterval method.

Remarks

The setInterval method continuously evaluates the specified expression until the timer is removed with the clearInterval method.

In versions earlier than Microsoft Internet Explorer 5, the first argument of setInterval must be a string. Evaluation of the string is deferred until the specified interval elapses.

As of Internet Explorer 5, the first argument of setInterval can be passed as a string or as a function pointer.

To pass a function as a string, be sure to append the function name with parentheses.

window.setInterval("someFunction()", 5000);

When passing a function pointer, do not include the parentheses.

window.setInterval(someFunction, 5000);

To retrieve a function pointer, use code as shown in the following example:

function callback()
{
    alert("callback");
}

function callback2()
{
    alert("callback2");
}

function chooseCallback(iChoice)
{
    switch (iChoice)
    {
    case 0:
        return callback;
    case 1:
        return callback2;
    default:
        return "";
    }	
}

// If i is 0, callback is invoked after 5 seconds.
// If i is 1, callback2 is invoked.
// Otherwise, the timer is not set.
window.setInterval(chooseCallback(i), 5000);

When you use the setInterval method with Introduction to DHTML Behaviors, the value of vCode should be a function pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary document.

Examples

This example uses the setInterval method to create a Dynamic HTML (DHTML) clock. A variable is assigned to the interval, and can be used as a reference to stop the interval by using the clearInterval method.

var oInterval = "";

function fnStartInterval(){
   oInterval = window.setInterval("fnRecycle()",1000);
}
function fnRecycle(){
   // Code to display hours, minutes, and seconds.
}
This feature requires Microsoft Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

The next example shows how two different closures can refer to the same variable

function AttachEvent(obj,evt,fnc,useCapture)
{
	if (obj.addEventListener) {
		obj.addEventListener(evt, fnc, useCapture ? true : false);
		return true;
	}
	else if (obj.attachEvent) {
		return obj.attachEvent("on"+evt, fnc);
	}
	else
		return false;	
} 
 
// To pass parameters to a function in SetTimeout or SetInterval,
// use a closure function. The inner function refers to a variable
// named outside the function scope. 
 
// The first example of a closure passes the variable to a named function.
function startTimer() {
	var div = document.getElementById('currentTime');
	setTimeout(function(){doClock(div)},200);
}
// The second example also uses a closure, by referring to an argument passed to the function.
function doClock(obj) {
	setInterval(function(){obj.innerHTML=(new Date()).toLocaleString()},200);
}
// This example demonstrates that more than one closure can refer to the same variable.
function startCounter() {
	var div = document.getElementById('counter');
	var count = 0;
	setInterval(function(){count++},143);
	setInterval(function(){div.innerHTML=count},667);
}
 
AttachEvent(window,'load',startTimer);
AttachEvent(window,'load',startCounter);
This feature requires Microsoft Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

There is no public standard that applies to this method.

Applies To

window, Window Constructor

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
failure mode      Mr. Raymond Kenneth Petry   |   Edit   |   Show History
This method-event fails to fire while other events are occurring, eg. during mouse motion over a DIV that invokes a dozen expression(), the interval-counter stops ... (not merely slow or delayed).

Even without mouse motion, popup invocation slows it too ~1 sec. / 250 expression().

REMEDY SUGGESTION: Watch Time%100 in style=whatever:expression(yourfunction(),default). (The last evaluation in the parenthetic-list is returned to the style.)

N.B. This method-event cannot access the mouse-pointer-position coordinates.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker