阅读(251) (0)

AngularJS 计时器

2017-07-19 17:47:06 更新

$timeout 服务是 ng 对 window.setTimeout() 的封装,它使用 promise 统一了计时器的回调行为:

angular.module('app', [], angular.noop)
.controller('TestCtrl', function($scope){
  var p = $timeout(function(){console.log('haha')}, 5000);
  p.then(function(){console.log('x')});
  //$timeout.cancel(p);
});

使用 $timeout.cancel() 可以取消计时器。