Skip to main content

Bug Tracker

Side navigation

#8935 closed bug (worksforme)

Opened April 21, 2011 09:02AM UTC

Closed April 28, 2011 09:36PM UTC

getJSON closure problem?

Reported by: cch_plus@126.com Owned by: cch_plus@126.com
Priority: low Milestone: 1.next
Component: ajax Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:
Description

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!

});

}

Attachments (0)
Change History (2)

Changed April 21, 2011 09:11AM UTC by ajpiano comment:1

owner: → cch_plus@126.com
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.

Changed April 28, 2011 09:36PM UTC by addyosmani comment:2

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).