悬停在幻灯片上时如何暂停角度间隔计时器

时间:2015-03-13 18:41:00

标签: javascript css angularjs

为什么我可以在我的代码中放置一个事件监听器,以便当鼠标光标悬停在其中一个幻灯片上时,计时器停止暂停?

flickr

$scope.jobNotification = 0;

var timer;
$scope.startAuto = function() {
    timer = $interval(function(){
        $scope.jobNotification = ($scope.jobNotification + 1) % $scope.jobs.length;
    }, 5000);
};

$scope.isActive = function (index) {
    return $scope.jobNotification === index;

 };

$scope.showJobNotification = function (index) {
    if (timer){
        $interval.cancel(timer);
        $scope.startAuto();
    }
    $scope.jobNotification = index;
};

1 个答案:

答案 0 :(得分:0)

您可以使用ng-mouseenter和ng-mouseleave指令来停止和启动计时器,更新了plunker http://plnkr.co/edit/09bf3T?p=preview

$scope.stopAuto = function() {
  console.log('tickCtrl.stopAuto() triggered');
  if(timer) {
      $interval.cancel(timer);
      timer = undefined;        
  }
}

<ul ng-mouseover="stopAuto()" ng-mouseleave="startAuto()">
    <li data-ng-repeat="job in jobs" ng-show="isActive($index)">
        <h1>{{job.jobTitle}}</h1>
        <p>{{job.sector}}</p>
    </li>
</ul>