4

I have an array: result[i]. I'd like to loop through each field in the array and append it to an element in my page.

$("tr:first").after(result[i]);

But I'd like this to happen with a delay.

Been trying to grock how queues work with each loops and a delay, but I just can't seem to work it out. I can get a delay, but only before they're all appended.

Thanks in advance.

0

4 Answers 4

7

Try queue:

$.each(result, function(idx, val) {
    $("tr:first").delay(1000).queue(function(next) {
        $(this).after(val);
        next();
    });
});

Just to be complete, this is for jQuery 1.4. In earlier versions, the callback should look like:

function() {
    // do whatever here
    $(this).dequeue();
}
Sign up to request clarification or add additional context in comments.

6 Comments

It needed $(this). before the after to work, but it only appended the first one.
@jackreichert: Yep, I fixed it. Should work now (assuming you use jQuery 1.4).
Sweet. Shouldn't this be function(next) { after(val); next(); } ?
perfect, on to the next challenge =) randomizing the delay. But I'm pretty sure that Math.random will do that for me. Thanks for the help!
@Victor Nicollet: Uhm are you referring to the actual version? I already fixed the next thing. Or what do you mean?
|
2

Behold, the power of recursion:

(function append(i) {
  if (i >= result.length) return;
  $('tr:first').after(result[i]);
  setTimeout(function(){append(i+1)},1000);
})(0);

You may add an additional setTimeout depending on whether you need the first item to appear immediately or after a delay.

1 Comment

It's defining an anonymous (yes, silly name) function called append, and calling it straight away with an argument of 0. The () around the function are there to make sure it's an anonymous function - otherwise, the parser might mistake it for a function definition.
1
$(result).each(function(i) {
    $("tr:first").delay(1000).after(result[i]);
});

3 Comments

This would append all results at t=1000, instead of result one at t=1000, result two at t=2000, result three at t=3000 and so on.
depends how you look at the question
Actually, "I can get a delay, but only before they're all appended." eliminates your interpretation. Look harder. ;-)
1

Another way you could handle this is by looping over your result array and setting up a bunch of setInterval or .delay() function calls, with delays that vary based upon the array index. For example:

for( var i = 0; i < result.length; i++ ){
  $('tr:first').delay(i*1000).after(result[i]);
}

To be sure, this isn't as good a solution as the recursive one provided by @Victor above, but it's another approach in case you don't like recursion for whatever reason.

2 Comments

Careful there. Using $.each() is safer than a for-loop due to the scoping rules for i
Sorry, never used $.each() for array spanning before, so I wouldn't feel good trying to write code off the cuff that uses it. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.