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?

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
confusedMind
  • 2,573
  • 7
  • 33
  • 74
  • Do these functions hit a server somewhere? – Ray Toal Jul 14 '14 at 02:54
  • @RayToal The functions get flights in real time from another site so "YES" if that is what you meant. – confusedMind Jul 14 '14 at 02:57
  • You could try using promises, that way you could make the calls concurrently and have the results returned when ready. – Justin McCraw Jul 14 '14 at 03:04
  • @JustinMcCraw that is the issue i have done all the work as above doing your way i have to redo everything. so i am looking for an easy way something like threading where i can run two threads like we do in c# – confusedMind Jul 14 '14 at 03:07
  • You should use Node.js Have a look at the answer here http://stackoverflow.com/questions/19120213/parallelizing-tasks-in-javascript – Sam Jul 14 '14 at 03:08
  • 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. – jfriend00 Jul 14 '14 at 03:16
  • @confusedMind You don't run "multiple threads at the same time" within the browser. The JavaScript engine in the browser is single threaded. Take a look at the implementation of these methods, as jfriend00 suggests, to see if they are improperly made synchronous. – Ray Toal Jul 14 '14 at 03:19
  • @RayToal Check out [Using web workers](https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers?redirectlocale=en-US&redirectslug=DOM%2FUsing_web_workers). – Alexei Levenkov Jul 14 '14 at 03:20
  • @jfriend00 that is the issue i made it so they should run same time but it does not . It runs getdepartflights(1,2); this function when done runs getreturnflights(1,2);. – confusedMind Jul 14 '14 at 03:24
  • @jfriend00" If, for some reason, these Ajax calls are set to be synchronous, then you need to change them to asynchronous" How do i do that, where to check? – confusedMind Jul 14 '14 at 03:36
  • I'm guessing your function calls are something generated by your framework, but there certainly has to be a way to do async ajax in your framework. You will either need someone who knows ASP or you will need to do some documentation research yourself. Here are two articles on it: http://runnable.com/Ukoh4PE5DdhXAAEr/how-to-use-async-in-asp-net-for-ajax and http://www.asp.net/ajax/documentation/live/Overview/AsynchronousLayerOverview.aspx. – jfriend00 Jul 14 '14 at 03:48
  • 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. – jfriend00 Jul 14 '14 at 03:49
  • @jfriend00 i found the issue if i set EnableSessionState=False/Readonly it works asyn .but it's is no good for me need to find another way. – confusedMind Jul 14 '14 at 03:51

0 Answers0