I am trying to make a jsonp call to server and here is how code looks like:
function myCallback(data) { var self = this; var currentId = data.ID; if(currentId == self.latestId){ console.log("latest request responded, do something"); self.latestId = new Date().getTime(); } else { console.log("old request responded, so we don't care"); } }; function makeCall() { var self = this; self.latestId = new Date().getTime(); var url = serverUrl + "&ID="+ self.latestId; $.ajax(url,{ dataType: "jsonp", timeout: 10000, cache: true, jsonp:"callback", jsonpCallback: "myCallback", error: function(id) { return function() { if(id == self.latestId) { console.log("latest request errored out");//might be due to timeout self.latestId = new Date().getTime(); // updating latestid so mycallback ignore this request } else { console.log("old request errored out, so we don't care anymore"); } } }(self.latestId) }); };
response from server look something like myCallback({some_data...});
When there is only one request get fired it works as expected, but when I fire 4-5 request with same data other then Id control neither go to myCallback nor to error, while server responded successfully in time. This doesn't happen consistently, so looks like a synchronization issue. Can there be any synchronization issue in using named callback while making multiple queries. In case of multiple queries only last one should win, but doesn't look like to be working here. Can it be a problem due to same callback name for every request?
I am kind of clueless about what is happening here, does someone has faced something like this.
EDIT: Looks like it is something to do with callback name for parallel requests which is mentioned Parallel JSONP requests in jQuery do not trigger multiple "callback events"?