Bug Tracker

Opened 12 years ago

Closed 12 years ago

#8935 closed bug (worksforme)

getJSON closure problem?

Reported by: [email protected] Owned by: [email protected]
Priority: low Milestone: 1.next
Component: ajax Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:


Bug: sample code var tempID; var sss = new Array("10001","10002"); for(var i = 0; i < sss.length; i++){

tempID = sss[i]; eee $.getJSON("/Rain/GetLastUploadingDateTime", {stationCode:tempID},function(rstData){

tempID unequal to "10001" jQuery 1.4.4 OK! }); }

Change History (2)

comment:1 Changed 12 years ago by ajpiano

Owner: set to [email protected]
Status: newpending

Thank you for your time and interest in helping the jQuery project, but it's unclear what you're reporting. Please provide a test case on jsfiddle that reproduces the issue and a more detailed explanation of what you believe is not working properly.

comment:2 Changed 12 years ago by addyosmani

Component: unfiledajax
Priority: undecidedlow
Resolution: worksforme
Status: pendingclosed

The issue with your code is that you were assuming the JSON/ajax request was successfully completing in a consistently quick manner each time you were iterating through your for loop, which isn't always going to be the case. Server latency can very easily vary and it's a better idea to change your logic to make use of the callback for the request so you can be certain one has completed before you go firing the other (this will also preserve the tempID values you're after). Please see here for a test case that demonstrates how you can achieve this: http://jsfiddle.net/addyosmani/LnKqv/ (there are probably better ways, but it's just an example).

Note: See TracTickets for help on using tickets.