docs(cookbook/component-communication): clear interval timer in OnDestroy

This commit is contained in:
Ward Bell 2016-02-22 02:40:57 -08:00
parent 2f82a51e0f
commit 823950b936
2 changed files with 15 additions and 11 deletions

View File

@ -1,18 +1,29 @@
// #docregion // #docregion
import {Component, EventEmitter, OnInit, Output} from 'angular2/core'; import {Component, OnInit, OnDestroy} from 'angular2/core';
@Component({ @Component({
selector:'countdown-timer', selector:'countdown-timer',
template: '<p>{{message}}</p>' template: '<p>{{message}}</p>'
}) })
export class CountdownTimerComponent implements OnInit { export class CountdownTimerComponent implements OnInit, OnDestroy {
intervalId = 0; intervalId = 0;
message = ''; message = '';
seconds = 11; seconds = 11;
clearTimer() {clearInterval(this.intervalId);}
ngOnInit() { this.start(); }
ngOnDestroy() { this.clearTimer(); }
start() { this._countDown(); }
stop() {
this.clearTimer();
this.message = `Holding at T-${this.seconds} seconds`;
}
private _countDown() { private _countDown() {
clearInterval(this.intervalId); this.clearTimer();
this.intervalId = setInterval(()=>{ this.intervalId = setInterval(()=>{
this.seconds -= 1; this.seconds -= 1;
if (this.seconds == 0) { if (this.seconds == 0) {
@ -23,11 +34,4 @@ export class CountdownTimerComponent implements OnInit {
} }
}, 1000); }, 1000);
} }
ngOnInit() { this.start(); }
start() { this._countDown(); }
stop() {
clearInterval(this.intervalId);
this.message = `Holding at T-${this.seconds} seconds`;
}
} }

View File

@ -2,7 +2,7 @@
"_listtype": "alpha", "_listtype": "alpha",
"index": { "index": {
"title": "Cookbooks" "title": "Cookbook"
}, },
"component-communication": { "component-communication": {