JavaScript
  Home arrow JavaScript arrow Page 5 - Using Timers in JavaScript
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using Timers in JavaScript
By: Nariman K, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 83
    2004-01-28

    Table of Contents:
  • Using Timers in JavaScript
  • Window Washer
  • New Year Blues
  • A Decent Interval
  • Turning Up the Volume
  • Sliding Around
  • A Long Wait

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Timers in JavaScript - Turning Up the Volume


    (Page 5 of 7 )

    Using a setInterval() function is identical to using a setTimeout() function in a loop. To illustrate this, consider the following example by rewriting the countdown clock example from a few pages back.


    <html>
    <head>
    <script language="JavaScript">
    var i
    =10;
    function 
    countDown() 
    {
     
    if(0)
     
    {
      document
    .forms[0].elements[0].value=i;
      i 
    i-1;
     
    }
     
    else 
     
    {
      alert
    ("Happy New Year!");
     
    }
    }
    window.setInterval("countDown()"1000);
    </script>

    </head>
    <body>
    <form>
    <input type="text" name="counter" size="3">
    </form>
    </body>
    </html>

    In this case, I've used the setInterval() method instead of the setTimeout() method to call the countDown() function over and over again. Note the difference between the version above and the one a few pages back - above, the call to setInterval() is outside the loop, whereas earlier, the call to setTimeout() was inside the loop.

    Here's one more example, this one making a <div> appear when the mouse moves over a link and automatically hiding it again after 3 seconds:


    <html>
    <head>
    <script language="JavaScript">
    function on
    () 
    {
     
    if(document.all
     
    {
      
    // make <div> visible
      document.all['volume'].style.visibility='visible';
      // after 3 seconds, make it invisible
      window.setInterval("document.all['volume'].style.visibility='hidden'",3000);
     } 
     
     if(document.layers) 
     {
      // make <div> visible
      document.volume.visibility='show';
      // after 3 seconds, make it invisible
      window.setInterval("document.volume.visibility='hide'",3000);
     } 
     
    if(!document.all && document.getElementById
     
    {
      
    // make <div> visible
      document.getElementById("volume").style.visibility="visible";
      // after 3 seconds, make it invisible
    window.setInterval("document.getElementById('volume').style.visibility='hidd
    en'"
    ,3000);
     

    }
    </script>

    </head>
    <body>
    <a href="#" onMouseOver="javascript:on();">Show Volume Control</a>
    <div id="volume" style="color:white; font-face:Arial; background:black;
    position:absolute; left:550; top:200; height:150; visibility:hidden;">
    Volume<br> control<br> slider<br> here
    </div>
    </body>
    </html>

    More JavaScript Articles
    More By Nariman K, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway