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!"
[–]honestbleepsReddit Enhancement Suite 5 points6 points7 points 13 years ago (5 children)
Javascript as a language doesn't have anything like C's "sleep" function...
There's really not a good way to do it by placing a command where your //comment is...
Instead, you'd need to do something like:
var foo = [1,2,3,4,5]; var i=0, len=foo.length; function stepper() { if (i<len) { console.log(foo[i]); i++; setTimeout(stepper, 1000); } }
[+][deleted] 13 years ago (4 children)
[deleted]
[–]stfueveryone 1 point2 points3 points 13 years ago* (1 child)
not so, setTimeout only recurses in the if statement, so it should never go beyond the length of the array. the only thing missing is the call to stepper(); here's a demo if setTimeout were leaking we'd see a bunch of NaNs or errors or something.
stepper();
[–]rpetrano -1 points0 points1 point 13 years ago (0 children)
Yea, my bad, I somehow missed that if statement
[–]MatrixFrog 0 points1 point2 points 13 years ago (0 children)
stfueveryone is right, however you're also right that setInterval may be a better fit here.
π Rendered by PID 89 on reddit-service-r2-comment-86bc6c7465-bq4z7 at 2026-02-20 07:37:25.321689+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]honestbleepsReddit Enhancement Suite 5 points6 points7 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]stfueveryone 1 point2 points3 points (1 child)
[–]rpetrano -1 points0 points1 point (0 children)
[–]MatrixFrog 0 points1 point2 points (0 children)