gpt4 book ai didi

javascript - for循环内的AngularJS $timeout

转载 作者:行者123 更新时间:2023-12-03 06:43:32 24 4
gpt4 key购买 nike

我正在尝试构建一个任务处理类型的 SPA。这个想法是有 10 个步骤,用户单击提交,然后将调用一个服务来开始一个进程,UI 将等待。一旦得到响应,就会应用一种风格来判断成功或失败。不管怎样,现在我正试图模拟这种行为,但我似乎无法让我的 Angular 正确,特别是 $timeout。下面是我的代码,请原谅我试图通过它来理解 Angular 的简化示例。

;function(angular) {
'use strict'

angular.module("workApp", [] )
.controller ("TaskController", function ($routeParams, workService, $timeout, $scope) {
var tc = this;

// These will be used in the view to flip the CSS to the appropriate color for success or failure
tc.step_0_status = "ready";
tc.step_1_status = "ready";
tc.step_2_status = "ready";

// trying this out, by storing my functions in a array, b/c I will have other uses in the end for this array.
var func_array = [
function(){step_0},
function(){step_1},
function(){step_2}
]

// This is where I am misunderstanding $timeout I guess. I simply want the loop to sleep for 3 seconds, before the next function is called.
$scope.startTasks = function results() {
for(var x = 0; x < func_array.length; x++) {
**$timeout(func_array[x](),3000);**
}

}

var step_0 = function() {
tc.step_0_status = "running"
}

var step_1 = function() {
tc.step_0_status = "completed";
tc.step_1_status = "running";
}

var step_2 = function() {
tc.step_1_status = "completed";
tc.step_2_status = "failed";
}


}
})(window.angular);

最佳答案

您可以使用 $interval创建您想要的 sleep 方法:

$interval(function(){
// do wahetever you need here
console.log('running');
},3000, func_array.length);

使用后请确保正确销毁它

关于javascript - for循环内的AngularJS $timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846151/

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