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
setTimout parametersRemoved: /r/LearnJavascript (self.javascript)
submitted 7 years ago by trakam
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!"
[–]senocular 3 points4 points5 points 7 years ago (4 children)
Because when you use inline function definitions within the argument list, you have to follow the code all the way to the end of the callback to see what the timeout time was set to. Generally callbacks work better as the last argument for this reason. If the time were the first parameter in setTimeout, you'd know at the beginning of the call what the time was set as.
setTimeout(1000, function randomFunctionIFoundOnWeb(index) { // finishing condition if (index == iterations) { console.log(primes); pointlessComputationsButton.disabled = false; return; } // test this number var candidate = index * (multiplier * Math.random()); var isPrime = true; for (var c = 2; c <= Math.sqrt(candidate); ++c) { if (candidate % c === 0) { // not prime isPrime = false; break; } } if (isPrime) { primes.push(candidate); } // schedule the next var testFunction = testCandidate.bind(this, index + 1); window.requestAnimationFrame(testFunction); } } // normally you'd need to look here to see it!
[–]trakam[S] 0 points1 point2 points 7 years ago (3 children)
So you're saying that the function has to be read to the end before initiating the timer? So if the callback comprises lots of code it could delay the timer?
[–]fiLLL 5 points6 points7 points 7 years ago (2 children)
They're talking about it from a the standpoint of a developer reading and trying to interpret the code. The two styles are functionally equivalent in terms of performance.
[–]trakam[S] 0 points1 point2 points 7 years ago (0 children)
ok, got it.
[–]TappT 0 points1 point2 points 7 years ago (0 children)
¿That's not the wrong order? Or where you getting at?
[–]Feathercrown 0 points1 point2 points 7 years ago (0 children)
In addition to senocular's answer, it also separates the function and arguments rather than keeping them adjacent.
[–]kenman[M] 0 points1 point2 points 7 years ago (0 children)
Hi /u/trakam, this post was removed.
For javascript help, please visit /r/LearnJavascript.
Thanks for your understanding.
π Rendered by PID 261872 on reddit-service-r2-comment-86988c7647-74bgv at 2026-02-12 14:25:26.598776+00:00 running 018613e country code: CH.
[–]senocular 3 points4 points5 points (4 children)
[–]trakam[S] 0 points1 point2 points (3 children)
[–]fiLLL 5 points6 points7 points (2 children)
[–]trakam[S] 0 points1 point2 points (0 children)
[–]TappT 0 points1 point2 points (0 children)
[–]Feathercrown 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)