3

How do I fire these four functions at exactly the same time;

fireThisNow1();
fireThisNow2();
fireThisNow3();
fireThisNow4();

2 Answers 2

5

You mean asynchronously yes? Try this:

setTimeout(function(){
    fireThisNow1();
}, 0);

setTimeout(function(){
    fireThisNow2();
}, 0);

setTimeout(function(){
    fireThisNow3();
}, 0);

setTimeout(function(){
    fireThisNow4();
}, 0);

Same (if parent object is window):

for(var i = 1; i < 5; i++){
    setTimeout(function(){
        window['fireThisNow' + i]();
    }, 0);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This will not fire the functions at exactly the same time, but one after each other.
So fireThisNow2 will fire after fireThisNow1 has started, and before fireThisNow1 has necessarily finished?
3

When you want to execute multiple functions in parallel (multi-threading), you could use WebWorkers.

2 Comments

Link to web workers tutorial: html5rocks.com/en/tutorials/workers/basics
caniuse.com/webworkers - Just a reminder that WebWorkers don't work in IE<10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.