Skip to content

Commit 7b051ad

Browse files
committed
Section 20: OnTick and OnComplete
1 parent 991f62b commit 7b051ad

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

20-Drawing-Animations/01-timer/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Timer {
1010
// Callbacks are optional (logic for that)
1111
if (callbacks) {
1212
this.onStart = callbacks.onStart
13+
this.onTick = callbacks.onTick
14+
this.onComplete = callbacks.onComplete
1315
}
1416

1517
// start(){}, Calling the constructor anytime the user clicks on the start button
@@ -39,11 +41,17 @@ class Timer {
3941
}
4042

4143
tick = () => {
42-
console.log('tick')
44+
// console.log('tick')
4345
if (this.timeRemaining <= 0) {
4446
this.pause()
47+
if(this.onComplete) {
48+
this.onComplete()
49+
}
4550
} else {
4651
this.timeRemaining = this.timeRemaining - 1
52+
if(this.onTick) {
53+
this.onTick()
54+
}
4755
}
4856
}
4957

@@ -66,8 +74,12 @@ const timer = new Timer(durationInput, startButton, pauseButton, {
6674
onStart() {
6775
console.log('Timer started')
6876
},
69-
onTick() {},
70-
onComplete() {},
77+
onTick() {
78+
console.log('Timer just ticked down');
79+
},
80+
onComplete() {
81+
console.log('Timer just completed');
82+
},
7183
})
7284
// timer.start()
7385

20-Drawing-Animations/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ const timer = new Timer(durationInput, startButton, pauseButton, {
156156
onStart() {
157157
console.log('Timer started')
158158
},
159-
onTick() {},
159+
onTick() {
160+
},
160161
onComplete() {},
161162
})
162163
```
163164

164165
```javascript
165-
class Timer {
166166
constructor(durationInput, startButton, pauseButton, callbacks) {
167167
this.durationInput = durationInput
168168
this.startButton = startButton
@@ -177,3 +177,30 @@ class Timer {
177177
this.pauseButton.addEventListener('click', this.pause)
178178
}
179179
```
180+
181+
182+
## 8. 16 OnTick and OnComplete
183+
184+
```javascript
185+
if (callbacks) {
186+
this.onStart = callbacks.onStart
187+
this.onTick = callbacks.onTick
188+
this.onComplete = callbacks.onComplete
189+
}
190+
```
191+
192+
```javascript
193+
tick = () => {
194+
if (this.timeRemaining <= 0) {
195+
this.pause()
196+
if(this.onComplete) {
197+
this.onComplete()
198+
}
199+
} else {
200+
this.timeRemaining = this.timeRemaining - 1
201+
if(this.onTick) {
202+
this.onTick()
203+
}
204+
}
205+
}
206+
```

0 commit comments

Comments
 (0)