2016-01-27 85 views
0

我想在我的for循環內延遲,但它不會真正起作用。 我已經試過了我的方式是在stackoverflow,但只是沒有人爲我想要的。延遲內部for循環無法正常工作

這是我有現在:

var iframeTimeout; 
var _length = $scope.iframes.src.length; 
for (var i = 0; i < _length; i++) { 

    // create a closure to preserve the value of "i" 
    (function (i) { 
     $scope.iframeVideo = false; 
     $scope.iframes.current = $scope.iframes.src[i]; 
     $timeout(function() { 
      if ((i + 1) == $scope.iframes.src.length) { 
       $interval.cancel(iframeInterval); 
       /*Change to the right animation class*/ 
       $rootScope.classess = { 
        pageClass: 'nextSlide' 
       } 
       currentId++; 
       /*More information about resetLoop at the function itself*/ 
       resetLoop(); 
      } else { 
       i++; 
       $scope.iframes.current = $scope.iframes.src[i]; 
      } 
     }, $scope.iframes.durationValue[i]); 

    }(i)); 

} 
alert("done"); 

這就是我想要的:所有的 首先我持有srcdurationdurationValue的對象。 我想播放我在我的物體中的兩個視頻。

  1. 我檢查有多少視頻的我有
  2. 我做iframeVideo可見(ngHide
  3. 我插入正確的標籤到我的div容器
  4. 它開始$timeout用正確的持續時間值
  5. 如果這樣做,如果有另一個視頻,請做同樣的事情。當它是最後一個視頻時,它應該激發一些代碼。

我希望一切都很清楚。


我也試過這樣:

var iframeInterval; 
var i = 0; 

$scope.iframeVideo = false; 
$scope.iframes.current = $scope.iframes.src[i]; 

iframeInterval = $interval(function() { 
    if ((i + 1) == $scope.iframes.src.length) { 
     $interval.cancel(iframeInterval); 
     /*Change to the right animation class*/ 
     $rootScope.classess = { 
      pageClass: 'nextSlide' 
     } 
     currentId++; 
     /*More information about resetLoop at the function itself*/ 
     resetLoop(); 
    } else { 
     i++; 
     $scope.iframes.current = $scope.iframes.src[i]; 
    } 
}, $scope.iframes.durationValue[i]) 
+0

'$ scope.iframes.durationValue [i]'的值是什麼,因爲這是所使用的延遲... –

+0

例如「10000」和「171000」的整數。 – SjaakvBrabant

+0

循環不能被延遲,你必須學習,JS [計時器](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers)是如何工作的。 – Teemu

回答

1

每個$timeout返回一個不同的承諾。要正確取消它們,您需要保存每個人。

本示例計劃從零開始的幾個後續操作。

var vm = $scope; 
    vm.playList = [] 
    vm.playList.push({name:"video1", duration:1200}); 
    vm.playList.push({name:"video2", duration:1300}); 
    vm.playList.push({name:"video3", duration:1400}); 
    vm.playList.push({name:"video4", duration:1500}); 

    vm.watchingList=[]; 

    var timeoutPromiseList = []; 
    vm.isPlaying = false; 

    vm.start = function() { 
     console.log("start"); 
     //ignore if already playing 
     if (vm.isPlaying) return; 
     //otherwise 
     vm.isPlaying = true; 
     var time = 0; 
     for (var i = 0; i < vm.playList.length; i++) { 
     //IIFE closure 
     (function (i,time) { 
      console.log(time); 
      var item = vm.playList[i]; 
      var p = $timeout(function(){playItem(item)}, time); 
      //push each promise to list 
      timeoutPromiseList.push(p); 
     })(i,time); 
     time += vm.playList[i].duration; 
     } 
     console.log(time); 
     var lastPromise = $timeout(function(){vm.stop()}, time); 
     //push last promise 
     timeoutPromiseList.push(lastPromise); 
    }; 

然後停止,取消所有的$timeout承諾。

vm.stop = function() { 
     console.log("stop"); 
     for (i=0; i<timeoutPromiseList.length; i++) { 
      $timeout.cancel(timeoutPromiseList[i]); 
     } 
     timeoutPromiseList = []; 
     vm.isPlaying = false; 
    }; 

DEMO on PLNKR

0

$timeout回報的承諾。你可以建立一個遞歸的承諾鏈,如this,所以下一段視頻會在少量時間後播放。