JavaScript setInterval

by top54u.com 24 Mar, 2008

JavaScript setInterval works similar to setTimeout function in JavaScript. setInterval method calls the specified expression after the specified interval of time in milliseconds.


In the previous articles we discussed about the use of setTimeout and clearTimeout methods and done some examples. Now we will learn the syntax and use of setInterval function of javascript:

 

JavaScript setInterval syntax:

 

setInterval( expression, msec, lang);

 

setInterval function takes 3 parameters:

Expression: Name of the function to call or the javascript expression to evaluate.

Msec:  Time in milliseconds after which the specified expression is evaluated.

Lang: Optional parameter to specify the scripting language e.g.: Jscript, Javascript, vbscript.

 

JavaScript setInterval Example:

 

HTML code:

<div id="myTime"></div>

<input type="button" value="start Interval" onclick="startInterval();"/>

 

JavaScript code:

    <script language="javascript">

    var interval;
   
    function startInterval()

    {

        interval = setInterval("startTime();",1000);

    }


    
    function startTime()

    {

        document.getElementById('myTime').innerHTML = Date();   

    }

    </script>

 

In the above JavaScript setInterval example, there are two functions startInterval that is called upon when a user clicks on start interval button. startInterval function has setInterval javascript method that calls the other function startTime after regular interval of time i.e. 1000 milliseconds (1 second).

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:40:47 AM




related videos.....
recent posts.....
top54u ezines.....