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">
</div>

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




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:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal