I have implemented a timer function in angularjs application as following:
$timeout($scope.startFilling, 30000)
$scope.onTimeout = function(){
$scope.counter1--; <= counter1 is initialized to 3
mytimeout = $timeout($scope.onTimeout,1000);
if($scope.counter1==0){
$timeout.cancel(mytimeout);
$scope.counter1=0
}
}
var mytimeout = $timeout($scope.onTimeout,1000);
I am basically calling a function after 30 seconds and the counter goes from 30 towards 0 and at 0 I cancel the timer.
I tried to implement it in angular2 as following:
start(){
let timer = Observable.timer(3000,1000);
this.myCounter = timer.subscribe(t=> this.startFilling(t));
}
startFilling(counter){
this.counter1=--counter;
if(this.counter1==0){
this.myCounter.unsubscribe();
}
}
But this isn't totally working correct. I am binding counter1 (which is initialized to 3)on my template. start() function is called a button click and as soon as it is called, I want to decrease the counter1 from 3 to 0 and after 3 seconds I want to call startFilling() function. How can I do it?