"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, August 9, 2010

Execute a Function at Regular Intervals using JavaScript

 The Window.setInterval() contains two parameters: the function name/function call with parameters
and the time of the timer. Using this, you can execute a function at regular intervals. Here’s a small
example:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Run the Same Function at Regular Intervals 
w/JavaScript</title>
    <script type="text/javascript">
        var cnt = 0;
        setInterval(printDuration, 3000);

        function printDuration() {
            cnt += 1;
            document.getElementById("para").innerHTML = cnt;
        }
    </script>
</head>
<body>
<div>
    <p id="para">0</p></div>
</body>
</html>
As you can see, we have set a timer of 3000 millisecond or 3 seconds. 
The printDuration() gets called after every 3 seconds and by manipulating t
he paragraph (para) element's innerHtml , we are able to print the value 
of the counter in it.


Have a nice day... 'N happy Coding :)

No comments: