如何停止Ajax请求(不等待,直到反应过来)? [英] How can i stop ajax request (don't wait untill response come)?
问题描述
如果我使用Ajax来发送请求和该请求需要很长时间.....如果我要送花药的要求怎么办?
当前的行为的第二个请求(我做了),等到第一个请求得到与响应。
注意: 我想要做的整个应用程序这种行为(任何新的要求执行,立即迫不及待旧的要首先完成) 用我的应用程序(阿贾克斯+ PHP + jQuery的+ Symfony的)
假设是第一请求多久时间:
$。阿贾克斯
({
键入:GET,
网址:URL1,
成功:函数(HTML)
{
// 做一点事
}
});
在任何时候,我想这个要求来执行和终止的第一个。
$。阿贾克斯
({
键入:POST,
网址:网址,
成功:函数(HTML)
{
//做一些其他的事情
}
});
VAR xhrReq;
xhrReq = $。阿贾克斯(...);
//那么如果你想停止rqest和退出,使用:
xhrReq.abort();
这是形式的手工工艺,但您可以添加一个全局XHR对象,并在每次请求对其进行测试。如果readyState为装,终止它:
VAR XHR;
VAR使用loadURL =功能(URL){
如果(XHR&安培;&安培; xhr.readyState大于0&安培;&安培; xhr.readyState 4;){
//有在管的请求时,中止
xhr.abort();
}
XHR = $获得(URL,函数(){
的console.log('成功',这一点);
});
};
使用loadURL('/ AJAX /');
If I use Ajax to send request and this request take long time ..... if I want to send anther request what should I do?
the current behaviour the second request (I did) waiting until the first request get with response.
NOTE : i want to do this behaviour on whole application (any new request execute immediately not wait the old one to be finished firstly) My application using (Ajax + PHP + jQuery + Symfony)
Assume that is the first request take long time:
$.ajax
({
type: "GET",
url: url1,
success: function (html)
{
// do some thing
}
});
In any time I want this request to execute and terminate the first one.
$.ajax
({
type: "POST",
url: url,
success: function (html)
{
// do some thing else
}
});
var xhrReq;
xhrReq = $.ajax(...);
// then if you want to stop the rqest and exit use :
xhrReq.abort();
It’s sort of a manual process, but you can add a global xhr object and test it on each request. If the readystate is "loading", abort it:
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/');
这篇关于如何停止Ajax请求(不等待,直到反应过来)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!