取消XHR请求 [英] Cancel XHR request

查看:64
本文介绍了取消XHR请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

function send(){
  $.get("/site/send.php", function(data){
    alert(data);
  }
}

在site/send.php中,我有:

In site/send.php I have:

sleep(1000)
echo "OK";

接下来我的js文件中有一个

Next I have in my js file:

send();
$("#click").click(function(){
  $.get("/site/reset.php", function(data){
    alert(data);
  }
})

以及在reset.php中:

and in reset.php:

echo "RESET";

在此示例中,我具有XHR直到睡眠<1000.在Firebug中,我看到了.我没有在send()函数中获得响应和成功,直到睡眠小于<.1000.可以,但是如果我使用.click(),我想取消此请求.

In this example I have XHR until sleep < 1000. In Firebug I see this. I don't have response and succes in function send() until sleep is < 1000. this is ok, but I would like cancel this request if I use .click().

现在我一直使用send()函数,直到她不停止(> 1000),然后我才开始使用.click()函数.为什么这不是异步的?

Now I have all time function send() and until she does not stop (> 1000) then I don't have start with function with .click(). Why this is not async?

这可能吗?也许可以取消网站上的所有请求?

Is this possible? Maybe it is possible to cancel all request on the site?

推荐答案

您必须保留对XHR对象的引用,以中止该对象.

You'll have to keep a reference to your XHR object, in order to abort it.

类似这样的东西:

var myXhr = null;
$("#click").click(function(){
  myXhr = $.get("/site/reset.php", function(data){
    alert(data);
  });
})

然后中止:

if (myXhr) {
  myXhr.abort();
}

尽管我真的不确定语法.

Although I'm really unsure about the syntax.

您可能希望查看一些 类似 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆