0

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?

13
  • Do these functions hit a server somewhere? Commented Jul 14, 2014 at 2:54
  • @RayToal The functions get flights in real time from another site so "YES" if that is what you meant. Commented Jul 14, 2014 at 2:57
  • You could try using promises, that way you could make the calls concurrently and have the results returned when ready. Commented Jul 14, 2014 at 3:04
  • 2
    Assuming that PageMethods.getFlights1() and PageMethods.getFlights2() are Ajax calls and they are not set to synchronous Ajax calls, then they should already be running at the same time. The getFlights1() request will be sent, then the getFlights2() 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. Commented Jul 14, 2014 at 3:16
  • 2
    Your 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. Commented Jul 14, 2014 at 3:49

0

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.