【发布时间】:2017-07-28 00:34:40
【问题描述】:
在下面的代码中,我想要实现的是每 10 秒调用一次return $http.get(newurl_3); 并刷新最后一个响应中的数据。我在不同的地方尝试了 setInterval 但无法使其工作。
它实际上是一个包含 5 个步骤的 Promise 链,总共有 100 多行代码。不确定将整个事情放在 setInterval 中是否是一个好习惯。
$http.get(url).then(function(data) {
// playing with data and returning a new promise
return $http.get(newurl_1);
}).then(function (data) {
return $http.get(newurl_2);
}).then(function (data) {
return $http.get(newurl_3);
}).then(function (data) {
return $http.get(newurl_4); // this call only needs to be refreshed.
}).then(function (data) {
// creating data array for UI.
// needs to be refreshed every 10 second to fetch updated data.
$scope.UI_presentation_Array = data...
})
【问题讨论】:
-
每隔 10 秒调用整个链
$http.get(url).then(function(data) { ...等 - 因为.then(function (data) {中的data不能神奇地改变 -
@JaromandaX 这是 5 条链,总共 150 行代码。 :|
-
放入一个函数中,在setInterval中调用该函数
-
@JaromandaX 只有最后 2 条链应该刷新。除了那个应用程序变得非常慢并且代码变得丑陋。
-
您展示了一条链。所以说 2 或 5 条链是没有意义的
标签: javascript jquery angularjs ajax promise