这是一个基于我之前的问题here的问题。
基本上,我使用$ q.all()方法来解析多个http调用。然后我过滤并合并两个数据源。
这一切都很有效。但我希望我的两个Feed中的一个每5分钟刷新一次。通常,我会通过将以下计时器附加到我的代码
的末尾来完成此操作var timer = $scope.intervalFunction = function() {
$timeout(function() {
/* function to call $http.get again */
$scope.intervalFunction();
}, 300000)
};
timer();
$timeout.cancel(timer);
我的问题是,由于我没有将http调用定义为单独的函数,并且我还“合并”两个源,我如何仅调用其中一个源,然后相应地更新而不进行多个列表项复制(这是许多尝试中发生在我身上的事情)。
谢谢!
JSFiddle here
答案 0 :(得分:1)
“我希望我的两个Feed之一每5分钟刷新一次”。我不确定你是否可以使用两个$q.all
请求中的一个来完成这个操作。我可以建议您在$q.all
包装器之外发出另一个请求,并在获得第一个$timeout
请求的结果后启动$q.all
。
需要说的是,我在这个解决方案中并不完全确定,因为我没有测试它,但希望这会有效:
$q.all(promises)
.then(function(response) {
metadata = response.metadataPromise.data;
metrics = response.metricsPromise.data;
joinMetadataAndMetrics();
requestEveryFiveMinutes();
})
.catch (error) (function (error) {
console.log(error);
});
function requestEveryFiveMinutes() {
$interval(function() {
// here you can make the needed http request
yourRequest()
.then(function() {
// handle the response as needed
})
.catch(function(err) {
console.log(err);
})
}, 300000);
}
不要忘记在控制器中注入$interval