Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: setTimeout()

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    setTimeout()

    Is there another JS function that I can use to force a time delay without having the code continue to run? I'm using the setTimeout() function but the call to this function doesn't stop the code flow. I need to stop the code flow while waiting. I guess I need a Sleep() type of JS function.

    What I'm trying to do is display some blank text on the screen using a for loop (I'm using &nbsp;<BR> to give the appearance of "opening up" a vertical window section in the browser) and I need this 'text' to complete before allowing the following code in the function to execute. The following code will display a Table with rows of data. I'm trying to use a timing function to give a visual impression of a window opening up just before the data displays.

    Thanks...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,010
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    There is no sleep() method in Javascript. The following is a horrible work-around kludge which charges the CPU 100% and freezes all the other Javascripts on the page.

    Code:
    <input type = "text" name = "txt1" onblur = "snooze(5)">
    
    <script type = "text/javascript">
    
    function snooze(naptime){
    naptime = naptime * 1000;
    var sleeping = true;
    var now = new Date();
    var alarm;
    var startingMSeconds = now.getTime();
    
    // alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
    
    while(sleeping){
    alarm = new Date();
    alarmMSeconds = alarm.getTime();
    if (alarmMSeconds - startingMSeconds > naptime){sleeping = false}
    }      
    alert ("Wake Up!");
    }
    
    </script>
    A much better way is to use setTimeout() to create a delay:-

    Code:
    function doStuff() {   //do some things or nothing  
    setTimeout(continueExecution, 10000) //wait ten seconds before continuing 
    }  
    function continueExecution() {    
    //finish doing things after the pause 
    }
    Quizmaster: In the early fifteenth century Owain Glyndwr led a rebellion against the ruling English in which country?
    Contestant: China.
    Last edited by Philip M; 08-01-2011 at 07:43 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    rfresh (08-01-2011)


  •  

    LinkBacks (?)


    Posting Permissions

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