import {bootstrap} from '@angular/platform-browser-dynamic'; import {Component} from '@angular/core'; import {NgIf} from '@angular/common'; import {TimerWrapper} from '@angular/core/src/facade/async'; @Component({ selector: 'async-app', template: `
{{val1}}
{{val2}}
{{val3}}
{{val4}}
`, directives: [NgIf] }) class AsyncApplication { val1: number = 0; val2: number = 0; val3: number = 0; val4: number = 0; timeoutId: any /** TODO #9100 */ = null; multiTimeoutId: any /** TODO #9100 */ = null; intervalId: any /** TODO #9100 */ = null; increment(): void { this.val1++; }; delayedIncrement(): void { this.cancelDelayedIncrement(); this.timeoutId = TimerWrapper.setTimeout(() => { this.val2++; this.timeoutId = null; }, 2000); }; multiDelayedIncrements(i: number): void { this.cancelMultiDelayedIncrements(); var self = this; function helper(_i: any /** TODO #9100 */) { if (_i <= 0) { self.multiTimeoutId = null; return; } self.multiTimeoutId = TimerWrapper.setTimeout(() => { self.val3++; helper(_i - 1); }, 500); } helper(i); }; periodicIncrement(): void { this.cancelPeriodicIncrement(); this.intervalId = TimerWrapper.setInterval(() => { this.val4++; }, 2000) }; cancelDelayedIncrement(): void { if (this.timeoutId != null) { TimerWrapper.clearTimeout(this.timeoutId); this.timeoutId = null; } }; cancelMultiDelayedIncrements(): void { if (this.multiTimeoutId != null) { TimerWrapper.clearTimeout(this.multiTimeoutId); this.multiTimeoutId = null; } }; cancelPeriodicIncrement(): void { if (this.intervalId != null) { TimerWrapper.clearInterval(this.intervalId); this.intervalId = null; } }; } export function main() { bootstrap(AsyncApplication); }