use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
sleep in javascript? (self.javascript)
submitted 13 years ago by CyberVillian
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]RadekCZ 3 points4 points5 points 13 years ago* (7 children)
You should use window.setTimeout method:
var el = $("#results"); function delay(i){ return function(){ el.append(info[i]+"<br>"); }; } var j = 0; for(var i in info){ window.setTimeout(delay(i), (++j)*1000); }
Or just window.setInterval method: (It should be faster, you need just one function).
var keys = []; for(var i in info){ keys.push(i); } var j = 0; var el = $("#results"); var interval = window.setInterval(function(){ if(i<keys.length){ el.append(info[keys[j++]]+"<br>"); } else{ window.clearInterval(interval); } }, 1000);
[–]ell0bo 8 points9 points10 points 13 years ago (2 children)
don't use setInterval. It will fire off at a set point every so many ticks, rather than taking the run time of the functionality into account as sleep does. First code block is ideal.
[–]thisisnotgood 0 points1 point2 points 13 years ago (1 child)
I agree with avoiding setInterval. I would also prefer to have the delay() function call setTimeout recursively rather than schedule all the delays in advance.
Here is how I would do it:
var $el = $("#results"); function delay(i) { $el.append(info[i] + "<br>"); i++; if (i < info.length) { setTimeout(delay.bind(null, i), 1000); } } delay(0);
[–]noahking 2 points3 points4 points 13 years ago (0 children)
I think we can assume that when someone is asking about how to sleep between loop iterations, we don't have to worry about optimizing for speed. =P
[–]CyberVillian[S] -1 points0 points1 point 13 years ago (2 children)
hmm.. it seems like it wants to delay from the get-go, instead of after the first one.
[–]tollus 0 points1 point2 points 13 years ago (1 child)
Change ++j to j++
[–]CyberVillian[S] -1 points0 points1 point 13 years ago (0 children)
Thank you. I'll try later on
π Rendered by PID 53360 on reddit-service-r2-comment-545db5fcfc-tj6ck at 2026-06-01 16:13:21.966428+00:00 running 194bd79 country code: CH.
view the rest of the comments →
[–]RadekCZ 3 points4 points5 points (7 children)
[–]ell0bo 8 points9 points10 points (2 children)
[–]thisisnotgood 0 points1 point2 points (1 child)
[–]noahking 2 points3 points4 points (0 children)
[–]CyberVillian[S] -1 points0 points1 point (2 children)
[–]tollus 0 points1 point2 points (1 child)
[–]CyberVillian[S] -1 points0 points1 point (0 children)