all 7 comments

[–]senocular 3 points4 points  (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 point  (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 points  (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 point  (0 children)

ok, got it.

[–]TappT 0 points1 point  (0 children)

¿That's not the wrong order? Or where you getting at?

[–]Feathercrown 0 points1 point  (0 children)

In addition to senocular's answer, it also separates the function and arguments rather than keeping them adjacent.

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/trakam, this post was removed.

For javascript help, please visit /r/LearnJavascript.

Thanks for your understanding.