-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patha-timer-target.js
More file actions
25 lines (19 loc) · 485 Bytes
/
Copy patha-timer-target.js
File metadata and controls
25 lines (19 loc) · 485 Bytes
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
'use strict';
// Hint: you need Node.js >= v19.0.0
class Timer extends EventTarget {
#counter = 0;
constructor(delay) {
super();
setInterval(() => {
const step = this.#counter++;
const data = { detail: { step } };
const event = new CustomEvent('step', data);
this.dispatchEvent(event);
}, delay);
}
}
// Usage
const timer = new Timer(1000);
timer.addEventListener('step', (event) => {
console.log({ event, detail: event.detail });
});