gpt4 book ai didi

javascript - AngularJS 使用 ng-resource 和多种服务器轮询方法

转载 作者:行者123 更新时间:2023-11-29 19:18:26 25 4
gpt4 key购买 nike

我正在使用基于此答案的方法:Server polling with AngularJS

但是当我有多种轮询方式时,如何设置这个更新呢?

这是我的服务的一个片段:

function pollingService($resource) {
return {
methodA: $resource(window.rootUrl + 'api/methodA', { para: '@para1' }, {
query: { method: 'GET', params: {}, isArray: false }
}),
methodB: $resource(window.rootUrl + 'api/methodB', {}, {
query: { method: 'GET', params: {}, isArray: false }
})
}
};

那么我如何设置 tick 方法来轮询这 2 个方法并且只创建 1 个轮询循环?

(function tick() {
$scope.method1 = pollingService.methodA.query(function () {
$timeout(tick, $scope.refreshRate);
});
$scope.method2 = pollingService.methodB.query(function () {
$timeout(tick, $scope.refreshRate);
});
})();

最佳答案

你可以使用 promises $q.all 函数:

var myTickFunc = function() {
return $q.all([pollingService.methodA.query().$promise, pollingService.methodA.query().$promise)
.then(function(result){
//Setup timer again
});

关于javascript - AngularJS 使用 ng-resource 和多种服务器轮询方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com