2016-06-15 11 views
0

私はSPAのタスク処理タイプを構築しようとしています。アイデアは10ステップあります。ユーザーがsubmitをクリックすると、サービスがプロセスを開始するために呼び出され、UIは待機します。応答が得られると、失敗の成功のためにスタイルが適用されます。とにかく、今、私はこの動作をモックしようとしていると、私は角度の正しい、特に$タイムアウトで得ることができない。私のコードは以下の通りであり、私が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); 
+0

を使用した後、適切にそれを破壊することを確認してください

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

を間隔](https://docs.angularjs.org/api/ng/service/$interval)で、タイムアウトではありません。 –

答えて

1

あなたが欲しいの睡眠法の作成に$intervalを使用することができます:$ [ちょうどあなたが使用する必要がある。この場合には、それ

+0

素晴らしいこれを試してアップデートを投稿します – Danman06

+0

いいです、それを答えとしてマークすることを忘れないでください!私はあなたのためにwotkedだとうれしいよ –

+0

うん、と私はコードとして表示するために私の返信を得るために夢中になることができる場合..:Pとにかく、ありがとう。私は本当に角張っている。私はあなたが$ intervalまでそのようなオブジェクト(func_array.length)を渡すことができるかどうかはわかりませんでした。それはかなり甘いです。 – Danman06