SunQuest
 
       JavaScript
  Home arrow JavaScript arrow Page 3 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 81
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using Timers in JavaScript - New Year Blues


    (Page 3 of 7 )

    Here's another example, this one containing two timers. The first one is set to close the window after 30 seconds. However, 10 seconds in, the second one will activate and ask the user to confirm the timeout. If the user does not confirm it, the timeout will be cancelled.


    <script language="JavaScript">
    // set timeout for window to close
    var win = setTimeout('window.close()', 30000);
    // set timeout for user input
    var check = setTimeout('checkStatus()', 10000);
    // ask if user wants window to close
    // if no, clear timeout for window to close
    function checkStatus()
    {
     if(!window.confirm("This window will shut in 20 seconds. Is that OK?"))
     { 
      window.clearTimeout(win);
     }
    }
    </script>

    Note that after the confirm() dialog bog is displayed and while it is waiting for a user click, all timers are suspended.

    Here's another example, this one using the setTimeout() method to create a countdown clock. Take a look:


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

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

    In this case, I'm using the setTimeout() method to call itself in a loop every second, and updating the value of the countdown clock on every iteration of the loop. When the countdown hits 0, an alert box is displayed.

    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 2 hosted by Hostway