2

I am aware there are other questions addressing the same issue, but I cannot find an answer I understand properly.

I am attempting to read an array of pages by looping through an array of page names, then using jQuery's $.get function to get the contents of the page. I believe it is due to loop closure that only the last callback of the loop fires. However, I do not understand a way around this or how to fix it, could someone please explain?

Here is the code:

function checkall2()
{
    i = 0;
    for(i in $servers) {
        $.get("servers/" + $servers[i], function(result) {
            alert(result + $servers[i]);
        });
    }
}
6
  • No, it is a globally declared javascript variable, it is just a simple string array containing different words for server names like test1, test2, test3 Commented Aug 5, 2012 at 13:49
  • some people prefix with $ to indicate its a jQuery object Commented Aug 5, 2012 at 13:49
  • I just do that for most of my javascript variables, as a habit I guess. Commented Aug 5, 2012 at 13:50
  • 1
    callback would only be triggered if request is successful, are you sure that's the case? Commented Aug 5, 2012 at 13:50
  • Not related to you question: Try to avoid using global variables if possible, wrapping them in a closure Commented Aug 5, 2012 at 13:54

5 Answers 5

4

It's a scope issue - i is getting "overwritten", and when finally the callbacks are triggered its value will be the same for all of them.

Since you are using jQuery, you can use an iterator instead:

$.each($servers, function(i, server) {
  $.get("servers/" + server, function(result) {
    alert(result + server);
  });
});

IMHO, that looks much clearer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I understandy why it is going wrong now, it is because i is the same for all of the callbacks by the time they fire, but your solution does not seem to work, for some reason the alert is never being called.
I replaced the $.get call with a setTimeout and it seems to work OK. There is no reason it'd fail in your case. Any errors?
No, no errors, just never gets to the alert, if I set a breakpoint in the debugger on the alert, it never reaches it
@PedroFerreira this wont work because server will contain the index of the array, you need a second param for the item -> $.each works different to $(selector).each api.jquery.com/jQuery.each
3

I think this is a scope problem - try and assign to a local within the loop, like this:

function checkall2()
{
    i = 0;
    for(i in $servers) {
        var server = $servers[i];
        $.get("servers/" + server, function(result) {
            alert(result + server);
        });
    }
}

edited to show working $.each -> http://api.jquery.com/jQuery.each/

function checkall2()
{
    $.each($servers, function(index, server) {
        $.get("servers/" + server, function(result) {
            alert(result + server);
        });
    });
}

Comments

0

You can't call async method in a loop, instead do this:

function checkall2(i)
{
    if(i< $servers.length) {
        $.get("servers/" + $servers[i], function(result) {
            alert(result + $servers[i]);
           checkall2(++i);
        });
    }
}

1 Comment

$.ajaxSetup({ async: false });
0
var $servers = [1,2,3];
function checkall2()
{
    i = 0;
    for(i in $servers) {
       $.get("servers/" + $servers[i], function(result) {
         alert(result + $servers[i]);
      });
    }
 }
 checkall2();

This works OK, on my machine. You sure your global variable is OK?

Comments

0

Ok I figured out why it was not working. Thank you Pedro for explaining the problem to me. So the variable result was different each time, there was no problem with that, it was just because the contents of all three files where identical, so once I changed them, I realised the result was different.

The reason why $servers[i] was the returning the same each time, was because once the loop had finished the callbacks where then called, and by that time i was at the end of the loop so it just returned $servers[2] three times (because 2 was the end of the loop).

Comments

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.