cancelling $http request angularjs

2019-07-01 19:42发布

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.

1条回答
Deceive 欺骗
2楼-- · 2019-07-01 20:11

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

查看更多
登录 后发表回答