Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate two JavaScript functions in parallel?

Anyone can tell me how to activate two (or more) JavaScript AJAX functions in parallel?

like image 467
Alex Avatar asked Feb 23 '10 13:02

Alex


2 Answers

Is this what you're looking for?

setTimeout('JsFunction1(val);', 0); 
setTimeout('JsFunction2(val);', 0);
like image 139
used2could Avatar answered Oct 04 '22 15:10

used2could


This is not possible. Javascript can only work in a single thread and there is no way to actually have two functions running in parallel. You need to make one call and then the other. The callbacks of these will be called (not necessarily in the same order with the invocation methods), when data have been returned or an error/timeout occurs. Only when one callback completes, will the second one be allowed to run.

Have also in mind that browsers restrict the number of active ajax calls. So, if you try to make too many ajax calls, one might wait (blocking all javascript code) for other calls to complete.

like image 33
kgiannakakis Avatar answered Oct 04 '22 16:10

kgiannakakis