我正在尝试在我的应用程序中设置间隔,并有类似这样的东西
html
<div class="text">
{{currentItem.name}}
</div>
<ul>
<li ng-repeat="item in items" ng-click="pickItem($index)">{{item.type}}</li>
</ul>
Controller
$scope.index = 0;
$scope.currentItem = $scope.items[$scope.index]; // set the first item
//The interval start 3 second after the app is loaded
$scope.startInt = $interval(function() {
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
}
}, 3000)
上面的代码将在页面加载 3 秒后开始间隔。我希望在页面首次加载时立即开始间隔。有没有办法做到这一点?非常感谢!
最佳答案
将匿名函数转换为命名函数。然后在使用区间之前调用函数
function IntervalFunction() {
$scope.currentItem = $scope.items[$scope.index];
$scope.index++;
if(scope.index === $scope.items.length) {
$scope.index=0;
}
}
IntervalFunctin();
$scope.startInt = $interval(IntervalFunction, 3000);
关于javascript - 在我的情况下如何设置间隔时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852464/