timer.js 689 B

123456789101112131415161718192021222324252627282930313233
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. ],
  6. function (angular, _, coreModule) {
  7. 'use strict';
  8. coreModule.default.service('timer', function($timeout) {
  9. // This service really just tracks a list of $timeout promises to give us a
  10. // method for cancelling them all when we need to
  11. var timers = [];
  12. this.register = function(promise) {
  13. timers.push(promise);
  14. return promise;
  15. };
  16. this.cancel = function(promise) {
  17. timers = _.without(timers,promise);
  18. $timeout.cancel(promise);
  19. };
  20. this.cancel_all = function() {
  21. _.each(timers, function(t) {
  22. $timeout.cancel(t);
  23. });
  24. timers = [];
  25. };
  26. });
  27. });