Sobes.tech
Middle+

Mis juhtub, kui uuesti tellite sama töötleja? Kuidas muutub kõnede järjekord?

sobes.tech AI

Vastus AI-lt

When re-subscribing to the same handler (for example, in an event system or on DOM events in JavaScript), this handler is usually added again to the list of listeners. This means that when the event occurs, the handler will be called as many times as it has been subscribed.

The order of calls does not change: handlers are called in the order they were added. If the same handler is added multiple times, it will be called multiple times in succession at the places where it was added.

Example in JavaScript:

const button = document.querySelector('button');
function handler() {
  console.log('Clicked');
}

button.addEventListener('click', handler);
button.addEventListener('click', handler);

// When clicking, the console will display:
// Clicked
// Clicked

To avoid duplicate subscriptions, it is usually checked whether the handler is already subscribed before adding it, or methods that do not add duplicates are used (for example, once or removing the handler before re-adding it).