Reputation: 119
I'm lost. I'm trying to do a class similar to below, at a specific time in the class I need to be able to report back via firing an event that something was completed. Yet it doesn't appear JS has events? Could someone shed some light on this topic for me please?
class MyClass {
constructor() {}
test() {
// Do Stuff
// RAISE EVENT: onTest
}
}
-------------------------
script.js
-------------------------
let myClass = new MyClass();
// Register the event listener here?
// myClass on event onTest () = {dostuff;}
myClass.test();
Upvotes: 2
Views: 983
Reputation: 15847
Technically you could use the built in events
and apply them to the document and just use addeventlistener
for that event.
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
class MyClass {
constructor() {}
test() {
document.dispatchEvent(new Event('tested'));
}
}
let myClass = new MyClass();
document.addEventListener('tested', function (e) { console.log("tested!") }, false);
myClass.test();
Upvotes: 3