Javascript clearInterval

by top54u.com 24 Mar, 2008

JavaScript clearInterval function clears the instance of timer calls of setInterval function. clearInterval stops the timer function of setInterval that calls the specified function after specified time intervals in milliseconds.

 

Javascript clearInterval syntax:

 

clearInterval( timerID );

 

clearInterval takes 1 parameter:

timerID: timer id of the timer instance initiated by setInterval function.

 

setInterval method returns the timerID of the instance initiated by it.

 

Example of JavaScript clearInterval with setInterval function:

 

HTML code:

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

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

<input type="button" value="stop Interval" onclick="stopInterval();"/>

 

JavaScript code:

  <script language="javascript">

    var interval;
   
    function startInterval()

    {

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

    }

    
    function startTime()

    {

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

    }

    function stopInterval()

    {

        clearInterval(interval);

    }

    </script>

 

In the above example of javascript clearInterval and setInterval 1 variable (interval) is declared as global to access its value in any function at any time when required. interval a global variable is used to store the timerID returned by setInterval function. clearTimeout function takes this interval variable as a timerID parameter that cancels the timer set by setInterval function.

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:42:33 AM




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