EventListener
interface
interface EventListenerCall signatures
Call signature
(evt: Event): voidThe EventListener interface represents a callback function to be called
whenever an event of a specific type occurs on a target object.
This is a basic event listener, represented by a simple function that receives an Event object as its only parameter.
Example
// Create an event listener function
const handleEvent = (event: Event) => {
console.log(`Event of type "${event.type}" occurred`);
console.log(`Event phase: ${event.eventPhase}`);
// Access event properties
if (event.cancelable) {
event.preventDefault();
}
};
// Attach the event listener to a target
const target = new EventTarget();
target.addEventListener('custom', handleEvent);
// Or create a listener inline
target.addEventListener('message', (event) => {
console.log('Message received:', event);
});