Make delicious recipes!

Timing scripts in Javascript

Timing Events: Scripts can be scheduled to run via the following four commands:

  1. Scheduling an event to be executed once in the future:

    var t_id = setTimeout ("script", milliseconds);

  2. Canceling a scheduled event:

    clearTimeout (t_id)

  3. Scheduling an event to be executed continuously at regular intervals in the future:

    var t_id = setInterval ("script", milliseconds);

  4. Canceling an event scheduled in #3 above:

    clearInterval (t_id)

Example: Following code increments a counter after every one second:

<div id="counter">

    var count=0;
    setInterval (function () {
        document.getElementById ("counter").innerHTML = "Counter value: " + count;    
    }, 1000);

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal