1

I'm using angular-timer and I'm just a little confused how to track its events. For example, I want to do something after time is up, but I can't see any events on console log.

  vm.add20Seconds = function() {
    $scope.$broadcast('timer-add-cd-seconds', 20);
  }

  $scope.$on('timer-add-cd-seconds', function (event, data) {
    console.log(data); // 'Some data'
  });

The console is empty.

https://github.com/siddii/angular-timer/blob/master/examples/angularjs-add-countdown-seconds.html

Croaton
  • 1,812
  • 3
  • 18
  • 28

2 Answers2

1

As the code given in link is not seems to be updated, I think you changed it to use controllerAs syntax. So your button html will use vm alias while calling controller method. Assuming you used ng-controller="MyAppController as vm"

Markup

<button type="button" ng-click="vm.add20Seconds()">Add 20 Seconds</button>

Else wanted to use $scope in your controller then simply change method to $scope.add20Seconds instead of vm.add20Seconds

Update

To get call a function after 20 seconds over, you could use $timeout service here, that will call and specified callback when mentioned $timeout completed.

Code

vm.add20Seconds = function() {
   $scope.$broadcast('timer-add-cd-seconds', 20);
}

var myCallbackAfterTimeout = function(){
   //add your code.
}

$scope.$on('timer-add-cd-seconds', function (event, data) {
 console.log(data); // 'Some data'
 $timeout(myCallbackAfterTimeout, data); //data is nothing but timeout milliseconds
});

Include $timeout dependency in your controller before using it.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Yes, I know. Adding seconds is working. I want to implement callback after time is up, but I can't see any events on 'timer-add-cd-seconds' broadcast. – Croaton Sep 12 '15 at 09:56
  • @VitalyFry You question looks confusing kindly update it..for your concern you should use `$timeout`/`$interval` – Pankaj Parkar Sep 12 '15 at 09:58
0

if you are looking for a good article about using the scope tree As A Publish And Subscribe (Pub/Sub) mechanism in angularJS please check this link

macrog
  • 2,085
  • 1
  • 22
  • 30