Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Is it possible to put a time delay in a for loop?
    JR

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I assume you mean with JavaScript. The answer is yes, but the outcome will not be what you expect.

    The way the setTimeout() feature works is that it creates a thread and sets it to execute at the elapsed time specified.

    The way setInterval() works is kind of like setTimeout(), only it sets a new timeout at the end of each timeout, continuously looping until the Interval instance is destroyed with window.clearInterval(interval);

    If you do something like this, for instance:
    Code:
    <script>
    function showNumber(i) {
        document.getElementById('xyz').innerHtml = i;
    }
    function loopNumbers() {
        for (var i = 0; i < 10; i++) {
            window.setTimeout("showNumber(" + i + ")", 1000);
        }
    }
    </script>
    <body onload="loopNumbers()">
        <p id="xyz"></p>
    </body>
    what will happen is that you'll set 10 timeout threads in a matter of milliseconds. Then, one second later, all 10 threads will attempt to execute virtually simultaneously.

    This is not what you want.

    You can, however, achieve the same functionality by creating an intelligent recursive timeout, which essentially is a function with an embedded timeout that calls itself after executing the desired functionality, passing with it a "persistent" value that tells itself how many loops have executed. When it gets to the desired number, it quits itself.

    Here's what I mean:
    Code:
    <script>
    var i = 0;
    function loopNumbers() {
        if (i < 10) {
            document.getElementById('xyz').innerHtml = i;
            window.setTimeout("loopNumbers();", 1000);
            i = i + 1;
        } else {
            i = 0;
        }
    }
    </script>
    <body onload="loopNumbers()">
        <p id="xyz"></p>
    </body>
    Because a function called with setTimeout() is delayed in execution, the way this works is that each setTimeout() gets delayed by being called in the function that also contains the logic to display the numbers.

    Notice how the variable i is declared outside the loopNumbers() function... this is so that each time the function calls itself, i remembers its value rather than resetting each time. That allows the function to terminate itself when i gets too high.

    Hope this helps ya out :-P

  4. #3
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks
    JR

  5. #4
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    BTW I have been doing that second method, but I got an 'out of memory' error. I will try it again.
    JR

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Try this:
    Code:
    <script>
    var i = 0;
    var timeout = null;
    function loopNumbers() {
        if (i < 10) {
            timeout = null;
            document.getElementById('xyz').innerHtml = i;
            timeout = window.setTimeout("loopNumbers();", 1000);
            i = i + 1;
        } else {
            i = 0;
        }
    }
    </script>
    <body onload="loopNumbers()">
        <p id="xyz"></p>
    </body>
    By making the timeout persistent and setting it to null in each loop, we're freeing up the memory taken by the new thread instance. That loop should theoretically run infinitely without any problems. If you have further difficulty (or require an exact timer), we should consider using the "setInterval()" method instead.

  7. #6
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks.. I resolved the problem eventually
    JR


Remove Ads

Posting Permissions

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

Search tags for this page

how to delay time in a loop in javascript
,
javascript delay in for loop
,

javascript delay loop

,
javascript loop delay time
,
time delay for loop
Click on a term to search for related topics.
All times are GMT -6. The time now is 11:19 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com