JavaScript
  Home arrow JavaScript arrow Page 7 - 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 - A Long Wait


    (Page 7 of 7 )

    Finally, I'll wrap things up with a more practical example, one which might come in handy in your Web development. In this next example, the task is to offer the user a file upload box, which he or she can use to upload files to a Web site. Once the user selects a file for upload and initiates the transfer, a dialog box should pop up with a message asking the user to be patient. Once the file upload completes, the dialog box (window) should automatically disappear.

    Now, there are a number of ways to implement this. Here's how I'm going to do it:

    1. Write the code to display a form for file upload, as well as the code to accept the file and transfer it to a data directory on the server. This will be implemented in PHP.

    2. Add code to the script above to throw up a pop-up window once the upload begins.

    3. Add code to the pop-up window to keep polling the parent window for file upload status. Once the upload has finished (signified by the parent browser window loading a new URL), the pop-up window will automatically close.

    First, the code for the form:


    <html>
    <head>
    </head>
    <body background="images/bkgd.gif" leftmargin="0" rightmargin="0"
    marginheight
    ="0" marginwidth="0" topmargin="0">
    <form action="upload.php" method="POST"
    onSubmit
    ="window.open('wait.html','resultsWindow','toolbar=no,location=no,
    directories=no,status=no,menubar=no,scrollbars=no,
    resizable=no,copyhistory=no,
    width=500,height=400');" 
    enctype="multipart/form-data" >
    Attach File
    <br>
    <
    input name="file" type="file" size="30" maxlength="50">
     
    <br>
    <
    input type="submit" value="Submit">
    </
    body>
    </html>

    Now, the server-side PHP script which processes the file submitted by the user through the form above:


    <?php
    // upload.php
    // upload the file to the server
    if($_FILES['file'][name] != "") 
    {
     if (is_uploaded_file($_FILES['file']['tmp_name'])) 
     {
      // if the file is clean, move it to final location
            if(file_exists($_FILES['file']['tmp_name'])) 
      {
       // check the size of the file
          if($_FILES['file']['size'] < $file_limit) 
       {
        // get the file name
           $NAME = $_FILES['file']['name'];
        // copy it to data directory
            if(copy($_FILES['file']['tmp_name'], $UPLOAD_LOCATION.$NAME))
        {
               // send browser to success page
                  header("Location: success.php");
         } 
       

      
    }
     

    }



    Once the upload is complete, the script above will redirect the browser to a success page (or an error page, if an error occurred).

    Notice in the form that I'm opening a new window when the user clicks the submit button. This new window will show the "be patient" message, and must also keep checking the parent window at pre-defined intervals to see if the transfer is complete. Here's the code:


    <html>
    <head>
    <script language="JavaScript">
    <!--
    // wait.html
    function checkOpenerStatus() {
        
    var strOpener opener.location.href;
        
    if(strOpener.indexOf("success.php") != -||
    strOpener
    .indexOf("error.php") != -1) {
         
    // I am going to close now
         self.close();
        }
    }
    // -->
    </script>
    </head>
    <
    body onLoad="setInterval('checkOpenerStatus()', 1000)">
    <
    center>
    <h1>
    Please wait
    your upload is being processed!
    <br>
    This window will close automatically once the upload is complete
    .
    </h1>
    </center>
    </
    body>
    </html>

    This script uses the setInterval() method to check the URL of the parent window every second and, when it detects that the parent has gone to the success or error page (indicating that the upload has completed), it automatically terminates itself with the self.close() method.

    And that's about all I have time for. Over the course of this article, I introduced you to JavaScript's four timing functions, showing you how to use the setTimeout() and setInterval() methods to call a function once after a pre-defined delay, or repeatedly at a pre-defined interval. That was the easy part, and I followed it up with a bunch of examples designed to showcase some of the things you can do with this power. Among the example applications I built: a browser tickertape, a countdown clock, a timed slideshow, a variable-speed page object, and a polling status window.

    I hope you had fun reading this article, and that the examples sparked off some ideas of where you can use these functions in your own scripts. 'Till next time, see ya!

    Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

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