/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {Component, NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; @Component({ selector: 'async-app', template: `
{{val1}}
{{val2}}
{{val3}}
{{val4}}
` }) class AsyncApplication { val1: number = 0; val2: number = 0; val3: number = 0; val4: number = 0; timeoutId: any = null; multiTimeoutId: any = null; intervalId: any = null; increment(): void { this.val1++; } delayedIncrement(): void { this.cancelDelayedIncrement(); this.timeoutId = setTimeout(() => { this.val2++; this.timeoutId = null; }, 2000); } multiDelayedIncrements(i: number): void { this.cancelMultiDelayedIncrements(); const self = this; function helper(_i: number) { if (_i <= 0) { self.multiTimeoutId = null; return; } self.multiTimeoutId = setTimeout(() => { self.val3++; helper(_i - 1); }, 500); } helper(i); } periodicIncrement(): void { this.cancelPeriodicIncrement(); this.intervalId = setInterval(() => this.val4++, 2000); } cancelDelayedIncrement(): void { if (this.timeoutId != null) { clearTimeout(this.timeoutId); this.timeoutId = null; } } cancelMultiDelayedIncrements(): void { if (this.multiTimeoutId != null) { clearTimeout(this.multiTimeoutId); this.multiTimeoutId = null; } } cancelPeriodicIncrement(): void { if (this.intervalId != null) { clearInterval(this.intervalId); this.intervalId = null; } } } @NgModule( {declarations: [AsyncApplication], bootstrap: [AsyncApplication], imports: [BrowserModule]}) class ExampleModule { } platformBrowserDynamic().bootstrapModule(ExampleModule);