Question

Well, again. I want to call a function inside a recursive function with all the arguments its meant to:

function foo(callback /* , callback args */) {
  var args;
  for(var i=1;i<arguments.length;i++) {
    args.push(arguments[i]);
  }
  // somehow set the timeout to foo
  callback.apply(args);
}

I already tried to apply to setTimeout too but doesnt seem to work:

args.unshift(foo, 100, callback);
setTimeout.apply(args);
Was it helpful?

Solution

The array of arguments should be the second argument to apply.

You need to add an argument to determine the value of this before it.

setTimeout.apply(window, args);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top