【发布时间】:2015-09-17 16:18:28
【问题描述】:
您好,我正在寻找一种中止 .$save() 请求的方法。我的资源对象目前有一个以毫秒为单位的超时,但是它没有按我的预期工作。超时将在 x 毫秒后触发 curCacheObj.$save 上的错误响应,但它实际上并没有中止 .$save() 方法,它仍然会发布到数据库!所以我想按照以下方式做一些事情:
$timeout(function() {
// abort .$save() function here
}, 30000);
但是我似乎找不到真正中止请求的方法。
目前我有以下代码(简化):
app.factory('Post', function($resource) {
return $resource('http://my_endpoint_here/post', {}, {save: {
method: 'POST',
timeout: 5000
}
});
});
app.service('scService', function ($window, Post, $cordovaProgress, $cordovaDialogs, $timeout) {
if (postCount > 0) {
$cordovaProgress.showSimple(false);
curCacheObj.$save(function (response) {
console.log("Post " + postCount + " sent!");
}, function (response) {
$cordovaProgress.hide();
console.log(curCacheObj);
$window.location.href= 'index.html';
return;
}).then(function() {
window.localStorage.removeItem("post" + postCount);
postCount --;
window.localStorage.setItem("localPostCount", postCount);
$cordovaProgress.hide();
console.log("this just ran");
scService.sendCache();
});
}
else {
$cordovaProgress.showSuccess(false, "Success!");
$timeout(function() {
$cordovaProgress.hide();
$window.location.href= 'index.html';
}, 1500);
}
});
【问题讨论】:
标签: angularjs asynchronous timeout abort