12

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

4
  • 3
    You already asked this twice: stackoverflow.com/questions/2312749 and stackoverflow.com/questions/2306334 Commented Feb 23, 2010 at 14:01
  • 1
    @John - These are not the same questions. They previous 2 are about calling functions with ASP. This is a much simpler version of the previous 2, I think this question should stay open. Commented Feb 23, 2010 at 14:03
  • but the problem is its not working parallel , one function after one only... i need some thing like picture in side the below link piecesofrakesh.blogspot.com/2009/03/… Commented Feb 23, 2010 at 14:12
  • 1
    The "picture in side the below link" is not parallel execution. It's parallel loading. You can trick the browser into loading javascript in parallel but you can't actually execute the functions in parallel. Commented Feb 23, 2010 at 14:56

7 Answers 7

17

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.

Sign up to request clarification or add additional context in comments.

Comments

6

Search for Web Workers. These are kind of a new feature in modern browsers and may not be available in old ones.

Comments

0

Is this what you're looking for?

setTimeout('JsFunction1(val);', 0); 
setTimeout('JsFunction2(val);', 0);

3 Comments

This will not execute them in parallel. JsFunction1 will always execute first, no matter what. setTimeout 0 just pushes code to the end of the execution stack.
you can't get anything in JS to actually execute at the EXACT same time but you can get them to execute in an "asynchronous" manner (I know JS is singled threaded so it's not really asynchronous, or at least it wasn't until Chrome came along). WTF is up with you trolling around for this post over two weeks later to make a random comment like that? Who has that kind of time?
This is the best answer in my opinion. I was thinking that you could use random delays if you really want random order.
0

use Web Workers to run tasks in Parallel

You can a tutorial here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Also, this library, which takes advantage of web workers, came up pretty fast on google: https://parallel.js.org/

Comments

-1

Using several 'setInterval' may make parallel running possible, though it may still run on a single core. The following code is an example of parallelizing a function func with an array of data 'datas'. To run it, use Parallel(func,datas) where func is the name of your global function and datas is an array of data each one as an input for func.

var i_array=new Array();
function Parallel(func,datas){
      $(datas).each(function(i,v){
             i_array[i]=setInterval(function(){
                         clearInterval(i_array[i]);
                         window[func](datas[i]);
                         },10);
             });
}

Here is a jsfiddle test. The time stamp in integer numbers show the two ajax were running in parallel.

1 Comment

your sample works, because you use ajax, and ajax runs async by default . if you set async:false for your ajax, you will see it won't work parallel.
-2

Use window.open() to call a new page. That page will call the first js function. After window.open() calls the second function, you are not technically waiting for the first function to complete. You just wait for the window.open() to execute and then the second will get execute.

Comments

-2

Javascript runs as a single thread, if the requests that you want to make doesn't have an IO, In that case its just not possible, if there's an IO operation involved, you can very well execute the two functions one after the other, the very nature of javascript will start executing the next function when it waits for IO. Usually in languages that support threading the same thing is achieved automatically during the CPU rest period for a thread.

2 Comments

Ever heard of callbacks?
@Jon Eustace Yes, if the two callback are sync and not async in such cases only one function will get executed at a time.

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.