I have a certain script that consists of ajax calls, and functions the use the data from the ajax calls to calculate something. There are basically two parts: a smaller part one, that has lot's of code and a few ajax calls; and a bigger part two, that has less code, but lots of ajax calls. Here is the 'simplified' version of my script:
function doStuff(i, moreWork){
var max = 20000*Math.random() + (200000+100000*Math.random())*moreWork;
for(var j = 0; j < max; j++){
$("#hello").text();
}
}
function ajaxCall(i, url){
if(i === 40){
return false;
}
$.get(url, function(data){
url = $(data).find("#url").attr("href");
if(i < 10){
//The first few calls cause a lot of work
doStuff(i, 1);
}
else{
//The others are much faster done
doStuff(i, 0);
}
ajaxCall(++i, url);
});
}
var url = "www.example.com"
ajaxCall(0, url);
If I change the ajax calls in timeouts, I can relatively see how long this takes to complete:
http://jsfiddle.net/XiozZe/KfW93/
Now, I think this script could be twice as fast, because the CPU is doing nothing, when he is waiting for the ajax calls. And while the CPU is doing work, there are ajax calls that can be called.
So I already did a slight improvement, I swapped the ajax call with the doStuff:
function doStuff(i, moreWork){
var max = 20000*Math.random() + (200000+100000*Math.random())*moreWork;
for(var j = 0; j < max; j++){
$("#hello").text();
}
}
function ajaxCall(i, url){
if(i === 40){
return false;
}
$.get(url, function(data){
url = $(data).find("#url").attr("href");
ajaxCall(++i, url);
if(i < 10){
//The first few calls cause a lot of work
doStuff(i, 1);
}
else{
//The others are much faster done
doStuff(i, 0);
}
});
}
var url = "www.example.com"
ajaxCall(0, url);
http://jsfiddle.net/XiozZe/KfW93/3/
This way, the script sends the second ajax call immediately after the first one was finished, and then begins with the code. The third ajax call is send after the first code and the second ajax call has finished.
Is there a way to make the two parts (ajax calls / work on code) sort of asynchronous? I want to keep sending and receiving ajax calls that don't have to wait for the code to finish. In the second the computer has to wait for the ajax call to come back, he uses that time to work on some code, and when the the call arrives, he interrupts the code for a moment to send the next one. My goal is that on every given moment, there is an ajax call on the way and my CPU is busy, or one of them is completely finished.
More explanation:
In the first example, the work scheme is this:
- Send ajax call 1
- Ajax: busy
- CPU: doing nothing
- Receive ajax call 1 && Start doStuff 1
- Ajax: doing nothing
- CPU: busy
- Finish doStuff 1 && Send ajax call 2
- Ajax: busy
- CPU: doing nothing
- Receive ajax call 2 && Start doStuff 2
- Ajax: doing nothing
- CPU: busy
- ...
In the second example, the work scheme is this:
- Send ajax call 1
- Ajax: busy
- CPU: doing nothing
- Receive ajax call 1 && Send ajax call 2 && Start doStuff 1
- Ajax: busy
- CPU: busy
- Finish ajax call 2
- Ajax: doing nothing
- CPU: busy
- Finish doStuff && Send ajax call 3 && Start doStuff 2
- Ajax: busy
- CPU: busy
- Finish ajax call 3
- Ajax: doing nothing
- CPU: busy
- Finish doStuff 2 && Send ajax call 4 && Start doStuff 3
- Ajax: busy
- CPU: busy
- ...
I want this:
- Send ajax call 1
- Ajax: busy
- CPU: doing nothing
- Receive ajax call 1 && Send ajax call 2 && Start doStuff 1
- Ajax: busy
- CPU: busy
- Finish ajax call 2 && Send ajax call 3
- Ajax: busy
- CPU: busy
- Finish doStuff 1 && Start doStuff 2
- Ajax: busy
- CPU: busy
- Finish ajax call 3 && Send ajax call 4
- Ajax: busy
- CPU: busy
- Finish doStuff 2 && Start doStuff 3
- Ajax: busy
- CPU: busy
- ...