JavaScript
  Home arrow JavaScript arrow More on Variables, Functions, and Flow Con...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

More on Variables, Functions, and Flow Control
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-12-14

    Table of Contents:
  • More on Variables, Functions, and Flow Control
  • 4.6 Branching Execution Based on Conditions
  • 4.7 Handling Script Errors Gracefully
  • 4.8 Improving Script Performance

  • 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


    More on Variables, Functions, and Flow Control


    (Page 1 of 4 )

    In this second part of a two-part series, you'll learn how to delay a function call, handle script errors gracefully, and more. It is excerpted from chapter 4 of the JavaScript & DHTML Cookbook, Second Edition, written by Danny Goodman (O'Reilly, 2008; ISBN: 0596514085). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    4.5 Delaying a Function Call

    Problem

    You want a function to run at a specified time in the near future.

    Solution

    Use the window.setTimeout() method to invoke a function one time after a delay of a number of milliseconds. You essentially set a timer to trigger a function of your choice. In its most common form, the function is referenced as a string, complete with parentheses, as in the following example:

      var timeoutID = setTimeout("myFunc()", 5000);

    The method returns an ID for the time-out operation and should be preserved in a global variable or property of a global object. If, at any time before the delayed function fires, you wish to abort the timer, invoke the clearTimeout() method with the time-out ID as the parameter:

      clearTimeout(timeoutID);

    Once the timer is set, other script processing may proceed as usual, so it is often a good idea to place the setTimeout() call as the final statement of a function.

    Discussion

    It's important to understand what the setTimeout() method doesn't do: it does not halt all processing in the manner of a delay that suspends activity until a certain time. Instead, it simply sets an internal countdown timer that executes the named function when the timer reaches zero. For example, if you are creating a slide show that should advance to another page after 15 seconds of inactivity from the user, you would initially set the timer via the load event handler for the page and the resetTimer() function:

      var timeoutID;
      function goNextPage() {
         
    location.href = "slide3.html";
      }
      function resetTimer() {
         
    clearTimeout(timeoutID);
          timeoutID = setTimeout("goNextPage()", 15000);
      }

    You would also set an event handler for, say, the mousemove event so that each time the user activates the mouse, the autotimer resets to 15 seconds:

      window.onmousemove = resetTimer;

    The resetTimer() function automatically cancels the previously set time-out before it triggers the goNextPage() function, and then it starts a new 15-second timer.

    If the function you are invoking via the delay requires parameters, you can assemble a string with the values, even if those values are in the form of variables within the function. But--and this is important--the variable values cannot be object references. Parameters must be in a form that will survive the conversion to the string needed for the first argument of the
    setTimeout() method. Recipe 8.4 demonstrates how you can convey names of DOM form-related objects as ways of passing an object reference. The tricky part is in keeping the quotes in order:

      function isEMailAddr(elem) {
         
    var str = elem.value;
         
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
         
    if (!str.match(re)) {
             
    alert("Verify the e-mail address format.");
             
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
             
    return false;
         
    } else {
              return true;
          }
      }

    In this example, the focusElement() function requires two parameters that are used to devise a valid reference to both a form object and a text input object. Both parameters of the focusElement() function are strings. Because the first argument of setTimeout() is, itself, a string, you have to force the "stringness" of the parameters to focusElement() by way of single quotes placed within the extended string concatenation sequence. (The zero milliseconds shown in the example is not a mistake for this application. Learn why in the Discussion for Recipe 8.4.)

    Conveniently, setTimeout() also accepts a function reference as its first parameter, thus opening up the possibility of using an anonymous function in that spot. Staying with the previous example, we can invoke multiple method calls within a single anonymous function, as in the following:

      setTimeout(function() {elem.focus(); elem.select();}, 0);

    This approach circumvents all the string machinations of the other format.

    If you are looking for a true delay between the execution of statements within a function or sections of a function, JavaScript has nothing comparable to commands available in some other programming languages. But you can accomplish the same result by dividing the original function into multiple functions--one function for each section that is to run after a delay. Link the end of one function to the next by ending each function with setTimeout(), which invokes the next function in sequence after the desired amount of time:

      function mainFunc() {
         
    // initial statements here
         
    setTimeout("funcPart2()", 10000);
     
    }
     
    function funcPart2() {
         
    // initial statements here
         
    setTimeout("finishFunc()", 5000);
      }
      function finishFunc() {
         
    // final batch of statements here
      }

    The related functions don't have to be located adjacent to each other in the source code. If all related functions need to operate on the same set of values, you can cascade the value as parameters (provided the parameters can be represented as nonobject values), or you can preserve them as global variables. If the values are related, it may be a good reason to define a custom object with values assigned to labeled properties of that object to make it easier to see at a glance what each function segment is doing with or to the values.

    Another JavaScript method, setInterval(), operates much like setTimeout(), but repeatedly invokes the target function until told to stop (via the clearInterval() method). The second parameter (an integer in milliseconds) controls the amount of time between calls to the target function.

    See Also

    Recipe 8.4 for using setTimeout() to keep script execution synchronized; Recipe 12.6 for an example of using a self-contained counter variable in a repeatedly invoked function to execute itself a fixed number of times; Recipes 13.9 and 13.10 for applications of setInterval() in animation.

    More JavaScript Articles
    More By O'Reilly Media


     

    JAVASCRIPT ARTICLES

    - Dynamic Drop-down Menus: Javascript Progress...
    - Creating Drop-down Menus Using Progressive E...
    - Building Slide Shows Using Progressive Enhan...
    - Form Validation with Progressive Enhancement...
    - Validating Web Forms: an Introduction to Pro...
    - Ajax Progressive Enhancement: Reading Remote...
    - Ajax Image Requests: Progresive Enhancement ...
    - jQuery Image Gallery: Working with Progressi...
    - Progressive Enhancement in an Image Gallery
    - Building a Drop-Down Menu with the HoverInte...
    - Using All of the HoverIntent jQuery Plug-in`...
    - Controlling Mouseout Events with HoverIntent...
    - Using the Interval Argument of the HoverInte...
    - Tweaking the Sensitivity of the HoverIntent ...
    - Introducing the HoverIntent jQuery Plug-in







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek