Availability: |
| |||||
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: | aFunction | A function object | ||||
aLanguage | A scripting language to execute the script source (MSIE only) | |||||
anInterval | A time interval in milliseconds | |||||
aSourceText | Some valid script source text | |||||
someArguments | The 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.
The functionality of this method is more limited in MSIE version 4. It does not support the passing of a function object and its arguments in separate parameters. However since you can define a fragment of JavaScript to call a function there are few circumstances where this will be a problem. It does prevent you from manufacturing a function object and calling it though.
On the other hand, implementing something that is non-portable across the MSIE and Netscape Navigator browsers is a bad idea anyway.
Be careful how you operate on these interval timers. You can store an identifier, which you can later use to cancel the timer. However, if the timer has already been fired, some browsers may crash due to you trying to cancel a non-existent timer.
Write your periodically executed code carefully to avoid memory leaks, as any repetitive code that leaks memory will rapidly slow the performance of the user's browser. It is not hard to leak as much as 50K bytes per loop in JavaScript. This will rapidly fill your memory and eventually the browser may crash. Even worse, memory leaks have a nasty habit of bringing the operating system down as well.
Prev | Home | Next |
Window.setHotkeys() | Up | Window.setResizable() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |