如果我使用Ajax发送请求,这个请求需要很长时间.....如果我想发送anther请求我该怎么办?
当前行为第二个请求(我做)等到第一个请求得到响应.
注意: 我想在整个应用程序上执行此行为(任何新请求立即执行而不是等待旧的首先完成)我的应用程序使用(Ajax + PHP + jQuery + Symfony)
假设这是第一个请求需要很长时间:
$.ajax
({
type: "GET",
url: url1,
success: function (html)
{
// do some thing
}
});
Run Code Online (Sandbox Code Playgroud)
在任何时候我都希望这个请求执行并终止第一个请求.
$.ajax
({
type: "POST",
url: url,
success: function (html)
{
// do some thing else
}
});
Run Code Online (Sandbox Code Playgroud)
var xhrReq;
xhrReq = $.ajax(...);
// then if you want to stop the rqest and exit use :
xhrReq.abort();
Run Code Online (Sandbox Code Playgroud)
这是一种手动过程,但您可以添加全局xhr对象并在每个请求上对其进行测试.如果readystate是"正在加载",则中止它:
var xhr;
var loadUrl = function(url) {
if ( xhr && xhr.readyState > 0 && xhr.readyState < 4 ) {
// there is a request in the pipe, abort
xhr.abort();
}
xhr = $.get(url, function() {
console.log('success', this);
});
};
loadUrl('/ajax/');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8487 次 |
| 最近记录: |