20

I want to do what setTimout does, manually, with no timeout.

setTimeout(function,0,args);

Just call a function and pass it an array of arguments, without knowing or caring how many arguments were there.

Basically I want to proxy the function call through another function.

I'm bad with terminology, sorry.

4

4 Answers 4

44
function f(a, b, c) { return a + b + c; }
alert(f.apply(f, ['hello', ' ', 'world']));
Sign up to request clarification or add additional context in comments.

Comments

7

In ES6:

function myFunc(a, b, c){
    console.log(a, b, c);
}

var args = [1, 2, 3];
myFunc(...args);

Comments

0

Sounds like you want a function with variable arguments as an argument. The best way to do this is either an explicit Array or object in general.

myFunction(callbackFunction, count, arrayOfOptions);

myFunction(callbackFunction, count, objectOfOptions);

Comments

0

Take a look at javascript's arguments variable. This is an array of all arguments passed in to the function.

So you can create your core function like this:

f = function () {
    for (arg in arguments) {
         // do stuff
    }
}

Then, create a new function with the right arguments:

f2 = function () {
    return f(arg1, arg2, arg3 /* etc. */);
}

And pass this function to setTimeout:

setTimeout(f2, 0);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.