0

I'm trying to send a few AJAX requests. It has to collect the data into a variable, and after it's done, it has to output the variable. If I don't do it synchronously, it will just output the variable before the requests are done. Is is possible to keep using the page, while the requests are running? The code could look something like this:

for (var i=0;i<10;i++) {
    $.ajax({
        type: "GET",
        url: "http://google.com",
        async: false,
        data: "",
        success:function(data){
            total += $(data);
            alert(total);
    }
}

That should load http://google.com/ 10 times, and output data into a variable called total. It is not my code I use (I like to keep it as private as possible), but only an example.

While it's loading Google 10 times, the page pauses because of async: false. Is is possible to make the page still work, and basically do the AJAX in the background? While it's loading, I can't do anything on the page.

Lemvig Kiggeren
  • 317
  • 1
  • 3
  • 13
  • 3
    `async: false,` please, NO! – A. Wolff Dec 06 '13 at 15:53
  • 2
    Set async to true, then use callbacks or deferred objects so that you only "output the variable" after the call has completed. Welcome to the world of async. – Jason P Dec 06 '13 at 15:53
  • 2
    Yes, set `async: true` and write your code to work asynchronously. – Kevin B Dec 06 '13 at 15:53
  • Do this and do that. Give me some examples, please. I come here for help. Setting it to true crashed my tab, so that won't work. – Lemvig Kiggeren Dec 06 '13 at 15:55
  • that's likely because you're trying to use `total` before all of the ajax requests are complete. We need to see more of your code. – Kevin B Dec 06 '13 at 15:56
  • please do some research before asking other to code it for you http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call Then, if you are stuck somewhere, ask a new question – A. Wolff Dec 06 '13 at 15:56
  • Flagged as off-topic, because to ask how to make an AJAX call work in the background (asynchronously) is the same as to ask how to make the mother a relative of yours. – emerson.marini Dec 06 '13 at 15:56
  • Exactly. How do I fix that? Some help would be appreciated. – Lemvig Kiggeren Dec 06 '13 at 15:57
  • It depends on what the end goal of your code is. with what you have provided we can't figure that out. – Kevin B Dec 06 '13 at 15:57
  • I want to load $(data) into the variable total. 10 times, as my code indicates. – Lemvig Kiggeren Dec 06 '13 at 15:59
  • Right. but what are you doing with it after. – Kevin B Dec 06 '13 at 16:00
  • Displaying it on a page..? Why does this matter to the code? :) – Lemvig Kiggeren Dec 06 '13 at 16:01
  • It matters because you can't simply return that variable from a function, or access it after the for loop. You can only do anything with it after all of the ajax requests have completed. – Kevin B Dec 06 '13 at 16:02
  • Exactly, that's what I am trying to do. Doesn't look like Stackoverflow is much help on this topic. I think I'll delete it. – Lemvig Kiggeren Dec 06 '13 at 16:03
  • There is something else that you can return from the function that gives you access to the value though. – Kevin B Dec 06 '13 at 16:04
  • 1
    @LemvigKiggeren seriously, all is explain here: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call So where are you stuck? If you want other to code for you, hire someone... – A. Wolff Dec 06 '13 at 16:04

1 Answers1

0

If I don't do it synchronously, it will just output the variable before the requests are done.

No, it will output the variable 10 times after each request is done. To avoid that, use counter for how many requests have finished:

for (var i=0;i<10;i++) {
    $.ajax({
        type: "GET",
        url: "http://google.com",
        async: true,
        data: "",
        success:function(data){
            total += $(data);
            if (!--i)
                alert(total);
        }
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375