I have two javascript functions basically on gets return Flights and the other gets departing flights. Currently, the way I have developed it runs one after another. So first it gets all depart flights than starts return flight which is very time-consuming.
Is there a way I can run the functions simultaneously.
Here is sample code:
function onpageLoad()
{
getdepartflights(1,2);
getreturnflights(1,2);
}
// THESE FUNCTIONS FURTHER CAN BE SUMMARIZED AS
function getdepartflights(flightID, flightType) {
PageMethods.getFlights1(flightID, flightType, OnGetMessageSuccessdepart);
}
function getreturnflights(flightID, flightType) {
PageMethods.getFlights2(flightID, flightType, OnGetMessageSuccessreturn);
}
And on OnGetMessageSuccessdepart and OnGetMessageSuccessreturn, I simply append the result to Literal so it makes my search result.
So any suggestion on how to improve this so both run the same time as they are independent of each other so should run.
UPDATED
Added this bit of code:
var processor = setInterval(function () {
alert("inside");
searhflightsdepart(i);
clearInterval(processor);
}, 100);
var processor2 = setInterval(function () {
alert("insidereturn");
searhflightsreturn(i);
clearInterval(processor2);
}, 100);
And it looks like it is now running the same time. Is this approach correct? I see two alerts inside and inside return instantly. But again when the first PageMethod is completed than it instantly goes into the second one. So what is stopping it to run async? What to check?
PageMethods.getFlights1()andPageMethods.getFlights2()are Ajax calls and they are not set to synchronous Ajax calls, then they should already be running at the same time. ThegetFlights1()request will be sent, then thegetFlights2()request will be sent immediately and then sometime later, each request will return its data. If, for some reason, these Ajax calls are set to be synchronous, then you need to change them to asynchronous so that they can both be in flight at the same time. It will also matter whether your server can process two requests at once or not.setInterval()approach isn't going to help anything. That is not the way to do it and doesn't help at all. Once your Ajax calls are asynchronous, you can just call them the way you originally had and they will be "in flight" at the same time. As long as your server can actually process them both at the same time, they will run in parallel.