javascript - 在我的情况下如何设置间隔时间

标签 javascript html css angularjs

我正在尝试在我的应用程序中设置间隔,并有类似这样的东西

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/

相关文章:

javascript - Coderbyte 上的数组加法 I JavaScript 函数

Javascript填充输入值

javascript - 使用 jquery 拖放图像

javascript - 将应用了 CSS3 滤镜的 Canvas 保存为图像

javascript - 使用 typescript 安装express.js 的正确方法是什么?

javascript - ngFor + ngModel : How can I unshift values to the array I am iterating?

javascript - 未捕获的类型错误 : Cannot read property 'reload' of undefined

html - 我的标签与下拉列表重叠......我想在它们之间添加空间

html - CSS Ul li 导航问题 : Can't get correct result