【发布时间】:2019-07-20 13:19:28
【问题描述】:
我正在尝试将 AJAX 调用批处理在一起,以便在它们全部完成后获得一个事件:
this.xhrs.push($.ajax({ type: "POST", url: this.options.firstUrl, data: this.options.data, datatype: 'json' }));
this.xhrs.push($.ajax({ type: "POST", url: this.options.secondUrl, data: this.options.data, datatype: 'json' }));
this.xhrs.push($.ajax({ type: "POST", url: this.options.thirdUrl, data: this.options.data, datatype: 'json' }));
this.xhrs.push($.ajax({ type: "POST", url: this.options.fourthUrl, data: this.options.data, datatype: 'json' }));
$.when
.call($, this.xhrs)
.done(function(first, second, third, fourth) {
...[process data]...
this.loading.hide();
this.map.fitBounds(this.bounds);
this.map.setZoom(this.map.getZoom() - 1);
}.bind(this));
但是该函数会立即调用,我也尝试过 .then 而不是 .done 但它也会立即触发。
绝对不是因为 AJAX 调用返回太快而无法注意到,因为其中一个调用需要 20 秒才能返回数据。
我做错了什么?
【问题讨论】:
标签: javascript jquery ajax deferred