SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot SlaveToTheMan's Avatar
    Join Date
    Aug 2001
    Location
    Land of OZ
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sleep function in JavaScript?

    Is there a sleep function in JavaScript that will stop the code execution? I would like the JS execution to delay/pause/sleep for a specified period of time during the execution of a link object's onclick event. I need this period of time in order to record Web Analytic's data.

    Steps I need are

    1) execute onclick event handler of a link
    2) execute JS assigned to onclick event of link object
    3) execute web analytics functions to record data
    4) delay JS execution to allow for web analytics data to be recorded
    5) return to normal link execution

  2. #2
    SitePoint Zealot SlaveToTheMan's Avatar
    Join Date
    Aug 2001
    Location
    Land of OZ
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this function on the internet (forgot where) to do this so I wanted to share it...BTW this page has interesting discussion on this http://www.daniweb.com/forums/thread47199.html

    Code JavaScript:
    /**
     * USE WISELY! It stops ALL JS execution in the browser (unlike the JS native 
     * settimeout function) for a period of time specified in milliseconds.  
     * 
     * This function's original intent is to be used
     * with onclick event functions in order to pause/delay JS execution
    * in order to ultimately delay a link's default action.
     * 
     * 1 second is 1,000 milliseconds
     * 
     * @param {Object} milliseconds
     */
    sleep = function(milliseconds) {
        var dt = new Date();
        while ((new Date()) - dt <= milliseconds) {
            /* Do nothing */
        }
    };

    Just call this at any point to pause the JS execution


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •