2016-01-21 47 views
0

我正在尋找一種從3開始倒數的方法,並從視頻元素中將圖片保存4次。所以countDownTimer將是3,2,1,3,2,1,3,2,1,3,2,1。見下面的代碼。現在,代碼將countDownTimer設置爲「3」,連續4次,然後倒數至-9。我想我明白爲什麼會發生這種情況,但是有沒有辦法讓定時器重置爲3以獲得所需的輸出?

//Counts down from 3 before each picture and adds each picture to the canvas 
    for(i = 0; i < 4; i++){ 
     $scope.countDownTimer = 3 //reset timer to 3 
     console.log($scope.countDownTimer); 

     //counts down from 3 
     $interval(function(){ 
     $scope.countDownTimer = $scope.countDownTimer - 1; 
     console.log($scope.countDownTimer); 
     }, 1000, 3) 
     //takes picture and adds to canvas 
     .then(function(){ 
     canvas.getContext('2d').drawImage(video, dx[i], dy[i], picWidth, picHeight); 
     }) 
    } 
+0

什麼是for循環?將創建4個間隔定時器同時運行 – charlietfl

+0

我想我想要做的是等待再次開始下一個迭代,直到$間隔函數完成。我沒有考慮同時啓動4個定時器,但這確實有道理! – mattc19

+0

所以我會更好的鏈接4 $ setinterval函數在一起? – mattc19

回答

1

如果你不關心印刷倒計時,你可以嘗試這樣的事情。

 //Counts down from 3 before each picture and adds each picture to the canvas 
      for(i = 0; i <= 4; i++){ 
       your code.. 

       //counts down from 3 
       $interval(function(){ 
//takes picture and adds to canvas 
       .then(function(){ 
       canvas.getContext('2d').drawImage(video, dx[i], dy[i], picWidth, picHeight); 
       }) 

       }, 3000, 3) 
      } 
+0

我希望能在屏幕上打印倒計時。 – mattc19