forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
96 lines (84 loc) · 2.65 KB
/
index.ts
File metadata and controls
96 lines (84 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {NgIf, bootstrap, Component, View} from 'angular2/bootstrap';
import {TimerWrapper} from 'angular2/src/facade/async';
@Component({selector: 'async-app'})
@View({
template: `
<div id='increment'>
<span class='val'>{{val1}}</span>
<button class='action' (click)="increment()">Increment</button>
</div>
<div id='delayedIncrement'>
<span class='val'>{{val2}}</span>
<button class='action' (click)="delayedIncrement()">Delayed Increment</button>
<button class='cancel' *ng-if="timeoutId != null" (click)="cancelDelayedIncrement()">Cancel</button>
</div>
<div id='multiDelayedIncrements'>
<span class='val'>{{val3}}</span>
<button class='action' (click)="multiDelayedIncrements(10)">10 Delayed Increments</button>
<button class='cancel' *ng-if="multiTimeoutId != null" (click)="cancelMultiDelayedIncrements()">Cancel</button>
</div>
<div id='periodicIncrement'>
<span class='val'>{{val4}}</span>
<button class='action' (click)="periodicIncrement()">Periodic Increment</button>
<button class='cancel' *ng-if="intervalId != null" (click)="cancelPeriodicIncrement()">Cancel</button>
</div>
`,
directives: [NgIf]
})
class AsyncApplication {
val1: number = 0;
val2: number = 0;
val3: number = 0;
val4: number = 0;
timeoutId = null;
multiTimeoutId = null;
intervalId = 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) {
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);
}