Sunday, March 29, 2015

Understanding Timers : setTimeout() and setInterval()


Browser provides a built-in scheduler which allows to setup function calls for execution after given period of time.


setTimeout :


The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

The Syntax is

var timerId = setTimeout(func | code, delay)

                        func | code -- Function variable or the string of code to execute.
                        delay -- Delay in 1000 ms . 1000 ms = 1 second


Cancelling the Execution :

 The ID value returned by setTimeout() is used as the parameter for          the clearTimeout() method.
            clearTimeout(timerId) 

Example : Try it yourself


setInterval :


The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

The Syntax is 

The setInterval(func|code, delay) method has same features as setTimeout.

var timerId = setInterval(func | code, delay)

Cancelling the Execution :

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

         clearInterval(timerId) 

Example : Try it yourself

Unknown Software Engineer

No comments:

Post a Comment