www.webdeveloper.com
Results 1 to 6 of 6

Thread: Timeout Delay prob

  1. #1
    Join Date
    Apr 2013
    Posts
    3

    Timeout Delay prob

    hello guys

    i am making a small game and have a problem with an animation caused by a timeout;
    Code:
    var time = 0;
    for (var i = 0; i < seq.length-1; i++) {
        ...
        // Move a Stone 
        setTimeout(function(){stone_move(...)},time);
        time += 800;
    };
    The stone_move() function is a jquery animation - the for loop let me move a sequence of stones.
    the problem on my script is that somehow the last movement of the sequence is shown immeadiatly (time=0)
    but it should be like:
    Code:
    t=0    stone_move(...); //1. stone moved
    t=800  stone_move(...); //2. stone moved
    t=1600 stone_move(...); //3. stone moved
    ...
    time of movement animation = 700ms
    --> at t=2500 everything should be finished by math
    do you have any clue where the dog is burried here? thx!

  2. #2
    Join Date
    Mar 2009
    Posts
    512
    What is the beginning value of "time"? You are starting the first timeout to wait for zero ms, so it starts immediately.
    Last edited by Tcobb; 04-25-2013 at 05:55 PM.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    could be the code you censor with "...", if that code uses "i" in any way.
    since all the "i"s are the same when the timeout fires, you can't use "i" in the timed-out code, owing to closure.

    other than that, it looks ok.
    from what i see, it's the first one, not the last one that fires immediately.

    the solution to code that does need to use i:
    Code:
    var time = 0;
    for (var i = 0; i < seq.length-1; i++) { (function(i){
        ...
        // Move a Stone 
        setTimeout(function(){stone_move(...)},time);
        time += 800;
    
    }(i));
    
    };
    Last edited by rnd me; 04-25-2013 at 06:16 PM.

  4. #4
    Join Date
    Apr 2013
    Posts
    3
    i meant to start the first movement immeadiatly (t=0) - thats why i increase the time after the animation.

    i do not fully understand the difference of your solution with bringing the timeout into a function. when i do the timeout "i" is not involved at all; but i will try it out when i come home...
    thanks in the meanwhile

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Abbadon View Post
    i do not fully understand the difference of your solution with bringing the timeout into a function. when i do the timeout "i" is not involved at all; but i will try it out when i come home...
    that's what was unclear. setTimeout problems in a loop are a very common gotcha in JS, almost always involving inadvertant closure or lack thereof.

    let me know if it helps and either way, we'll see what we can figure out.

  6. #6
    Join Date
    Apr 2013
    Posts
    3
    thank you 1000 times! the closure helped perfectly

    var time = 0;
    for (var i = 0; i < ende; i++) {
    (function(i,time){
    // local parameters that will be waiting for the setTimeout.
    setTimeout(function(){stone_move(...)},time);
    })(i,time);
    time += 800;
    };

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles