此代码来自$httpBackend源代码:
if (timeout > 0) { var timeoutId = $browserDefer(timeoutRequest, timeout);} else if (timeout && timeout.then) { timeout.then(timeoutRequest);}function timeoutRequest() { status = ABORTED; jsonpDone && jsonpDone(); xhr && xhr.abort();}timeout.then(timeoutRequest)表示在解决承诺(不拒绝)后,将调用timeoutRequest并中止xhr请求。
如果请求超时,则
reject.status === 0( 请注意:如果网络出现故障,则该值
reject.status也将等于0),例如:
app.run(function($http, $q, $timeout){ var deferred = $q.defer(); $http.get('/path/to/api', { timeout: deferred.promise }) .then(function(){ // success handler },function(reject){ // error handler if(reject.status === 0) { // $http timeout } else { // response error status from server } }); $timeout(function() { deferred.resolve(); // this aborts the request! }, 1000);});


