我想在我的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");
這就是我想要的:所有的 首先我持有src
,duration
和durationValue
的對象。 我想播放我在我的物體中的兩個視頻。
- 我檢查有多少視頻的我有
- 我做
iframeVideo
可見(ngHide
) - 我插入正確的標籤到我的div容器
- 它開始
$timeout
用正確的持續時間值 - 如果這樣做,如果有另一個視頻,請做同樣的事情。當它是最後一個視頻時,它應該激發一些代碼。
我希望一切都很清楚。
我也試過這樣:
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])
'$ scope.iframes.durationValue [i]'的值是什麼,因爲這是所使用的延遲... –
例如「10000」和「171000」的整數。 – SjaakvBrabant
循環不能被延遲,你必須學習,JS [計時器](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers)是如何工作的。 – Teemu