我正在尝试对服务器进行jsonp调用,这是代码的样子:
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)
});
};
来自服务器的响应看起来像myCallback();
当只有一个请求被触发时它按预期工作,但是当我用相同的数据激发4-5请求时,Id控制既没有转到myCallback也没有转错,而服务器及时响应成功 . 这不会持续发生,因此看起来像是同步问题 . 在进行多个查询时,使用命名回调时是否存在任何同步问题 . 在多个查询的情况下,只有最后一个应该赢,但看起来不像在这里工作 . 由于每个请求都有相同的回调名称,这可能是一个问题吗?
我对这里发生的事情毫无头绪,是否有人遇到过这样的事情 .
编辑:看起来它与并行请求的回调名称有关,提到Parallel JSONP requests in jQuery do not trigger multiple "callback events"?