New
- Add
Event API
.
const evt = new EventTarget();
evt.addEventListener("hello", (evt) => {
console.log(evt);
});
setTimeout(() => {
evt.dispatchEvent(new Event("hello"))
}, 2000);
// using CustomEvent
evt.addEventListener("custom", (evt) => {
console.log(evt.detail.text());
});
setTimeout(() => {
evt.dispatchEvent(new CustomEvent("custom", { detail: { text: () => "Hello From Event" } }))
}, 2000);