Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
Event Api
The TYPO3 JavaScript Event API enables JavaScript developers to have a stable event listening interface. The API takes care of common pitfalls like event delegation and clean event unbinding.
Event Binding
Each event strategy (see below) has two ways to bind a listener to an event:
Direct Binding
The event listener is bound to the element that triggers the event. This is done
by using the method bind
, which accepts any element, document
and
window
.
Example:
require(['TYPO3/CMS/Core/Event/RegularEvent'], function (RegularEvent) {
new RegularEvent('click', function (e) {
// Do something
}).bindTo(document.querySelector('#my-element'));
});
Event Delegation
The event listener is called if the event was triggered to any matching element inside its bound element.
Example:
require(['TYPO3/CMS/Core/Event/RegularEvent'], function (RegularEvent) {
new RegularEvent('click', function (e) {
// Do something
}).delegateTo(document, 'a[data-action="toggle"]');
});
The event listener is now called every time the element matching the selector
a
within document
is clicked.
Release an event
Since each event is an object instance, it's sufficient to call release
to
detach the event listener.
Example:
require(['TYPO3/CMS/Core/Event/RegularEvent'], function (RegularEvent) {
const clickEvent = new RegularEvent('click', function (e) {
// Do something
}).delegateTo(document, 'a[data-action="toggle"]');
// Do more stuff
clickEvent.release();
});
Event Strategies
The Event API brings several strategies to handle event listeners:
RegularEvent
The Regular
attaches a simple event listener to an event and element
and has no further tweaks. This is the common use-case for event handling.
Arguments:
event
(string) - the event to listen onName callback
(function) - the event listener
Example:
require(['TYPO3/CMS/Core/Event/RegularEvent'], function (RegularEvent) {
new RegularEvent('click', function (e) {
e.preventDefault();
window.location.reload();
}).bindTo(document.querySelector('#my-element'));
});
DebounceEvent
The Debounce
is most suitable if an event is triggered quite often
but executing the event listener is called only after a certain wait time.
Arguments:
event
(string) - the event to listen onName callback
(function) - the event listenerwait
(number) - the amount of milliseconds to wait before the event listener is calledimmediate
(boolean) - if true, the event listener is called right when the event started
Example:
require(['TYPO3/CMS/Core/Event/DebounceEvent'], function (DebounceEvent) {
new DebounceEvent('mousewheel', function (e) {
console.log('Triggered once after 250ms!');
}, 250).bindTo(document);
});
ThrottleEvent
Arguments:
event
(string) - the event to listen onName callback
(function) - the event listenerlimit
(number) - the amount of milliseconds to wait before the event listener is called
The Throttle
is similar to the Debounce
. The important
difference is that the event listener is called after the configured wait time
during the overall event time.
If an event time is about 2000ms and the wait time is configured to be 100ms, the event listener gets called up to 20 times in total (2000 / 100).
Example:
require(['TYPO3/CMS/Core/Event/ThrottleEvent'], function (ThrottleEvent) {
new ThrottleEvent('mousewheel', function (e) {
console.log('Triggered every 100ms!');
}, 100).bindTo(document);
});
RequestAnimationFrameEvent
The Request
binds its execution to the browser's
Request
API. It is suitable for event listeners that
manipulate the DOM.
Arguments:
event
(string) - the event to listen onName callback
(function) - the event listener
Example:
require(['TYPO3/CMS/Core/Event/RequestAnimationFrameEvent'], function (RequestAnimationFrameEvent) {
new RequestAnimationFrameEvent('mousewheel', function (e) {
console.log('Triggered every 16ms (= 60 FPS)!');
});
});