取消$ http请求angularjshttp、angularjs

2023-09-13 05:08:34 作者:酷是我的style

试图取消一个搜索输入框的$ HTTP请求,但似乎事先请求不会被得到中止。我跟着从其他堆栈溢出问题的例子。这里是我的JS code:

Trying to cancel an $http request on a search input box but the prior requests don't seem to be getting aborted. I followed the example from other stack overflow questions. Here is my JS code:

var canceler;
$scope.someFunction = function() {
      if(canceler)
         canceler.resolve();
      canceler = $q.defer();
      $http({method: 'GET', url:  service+url, timeout: canceler.promise}).
          success(function(data, status, headers, config) {  });
}

HTML

<input ng-change="someFunction" />

如果我输入两个字符的搜索框中的函数被调用两次,但初始请求仍能通过。所以这两个请求最终经历和响应时间是随机正在使用的响应悬而未决。难道在某些时候图书馆的改变?我有一段时间以前这方面的工作,但现在它不再是工作。

If I enter two characters into the search box the function is called twice but the initial request still goes through. So both requests end up going through and pending on the response time it is random which response is being used. Did the library change at some point? I had this working awhile ago but now it no longer is working.

推荐答案

我想事业,他们已删除了承诺超时选项,你现在只能给毫秒超时,但可能有一些错误的角度JS的版本有一个变通为,检查此链接的http://www.bennadel.com/blog/2624-aborting-an-ajax-request-in-angularjs-using-httpi.htm

i think there might be something wrong with the version of angular js cause they have removed the timeout option for promise and you can now only give timeout in milliseconds but there is a work around for that, check this link http://www.bennadel.com/blog/2624-aborting-an-ajax-request-in-angularjs-using-httpi.htm