Sunday, December 13, 2009

Javascript: setTimeout or setInterval

The setTimeout function delays for a specified time period and then triggers execution of a specified function. Once the function is triggered the setTimeout has finished. Use the clearTimeout function to terminate the execution of the setTimeout.
Example of use: Set a timelag before drop-down menu closes after a mouseout event.
Code: setTimeout("closeMenu()", delay)

The setInterval function also delays for a specified time before triggering the execution of a specific function. However, the command doesn't complete after triggering that function. Instead it waits for a specified time again and then triggers the function again. This process continues (at specified intervals) until page is unloaded or the clearInterval function is called.
Example of use: Slide-in and slide-out animations for the drop-down menu. At every interval, set length and opacity accordingly.
Code: setInterval("ddMenu()", interval)

No comments:

Post a Comment