for循环中的$ interval

时间:2016-01-21 03:13:44

标签: angularjs setinterval angular-promise

我正在寻找一种从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);
      })
    }

1 个答案:

答案 0 :(得分: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)
            }