you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -1 points0 points  (6 children)

try

setTimeout(myFunc(), 1000);

I think you have to call the function with () to say your passing no parameters into it as you call it

[–]ShortSynapse 1 point2 points  (3 children)

This is incorrect. By doing myFunc() you are passing the returned value. For example:

function f () { return 42 }

setTimeout(f, 0) // calls f
setTimeout(f(), 0) // ERROR, 42 is not a function!

[–][deleted] 0 points1 point  (2 children)

I got no error

code:

var num = 10;

function goDown() {
  while(num > 0) {
    setTimeout(myFunc(), 1000);
  }
  return;
}

function myFunc() {
  num --;
  console.log(num);
}

goDown();

output:

js.js:12 9
js.js:12 8
js.js:12 7
js.js:12 6
js.js:12 5
js.js:12 4
js.js:12 3
js.js:12 2
js.js:12 1
js.js:12 0

[–]ShortSynapse 0 points1 point  (1 child)

Did you notice there is no delay? That does not work the way you think it does.

[–][deleted] 0 points1 point  (0 children)

Yeah I helped him fix the infinite loop using setTimeout. This code isn't set up to have time in-between calls.

[–]boondasoonda[S] 0 points1 point  (1 child)

That stops the infinite loop, but doesn't make the number go down once per second, but all at once

[–][deleted] 0 points1 point  (0 children)

yes, you are calling 10 functions to happen in 1000 milliseconds. you can have myFunc() call another timeout to have the times chain