Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Throttle JS Event Listeners added by 3rd party library

I want to throttle function calls which are added as Event Listeners to the window.scroll function by a 3rd party library provided by an external supplied (cant be changed).

I figured out that the library causes some overhead by its scroll event listener, because if I remove the event handler, my page runs much smoother.

As I cannot directly control or change the external JS file, I thought to read the scroll-events attached to the Window and delete / rebind them again, but in a throttled format, as I have already the Underscore.js library in use.

I'm trying to read the Scroll events and than replace the function callback as a throttled version:

  jQuery(document).ready(function($) {
    let scrollEvents = $._data(window, 'events').scroll;
    for(evt of scrollEvents ) { 
      evt.handler = _.throttle(evt.handler, 200) 
    }
  });

Does not seem to bring the appropriate improvement. In the Webdeveloper Bar "Global Event Listeners" I still see the original event listeners attached, I do NOT see the Underscore Library (as intermediate layer) listed there.

What is potentially wrong with this code?

Thanks

EDIT Those events are added globally to the Window, see WebDev Screenshot: enter image description here and I run the above code within the WebDev console, so it is ran only after those events exist already.

AND $._data(window, 'events').scroll; shows ALL those 5 events, so jQuery should be able to change them, isnt it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Found a beautiful solution using Underscore.js, proxying the callback functions by a Throttler before adding it as Event Handler:

    var f_add = EventTarget.prototype.addEventListener;
    var f_remove = EventTarget.prototype.removeEventListener;

    EventTarget.prototype.addEventListener = function(type, fn, capture) {
        this.f = f_add;
        if(type == 'scroll' && typeof _ === 'function') 
            fn = _.throttle(fn, 350);
        this.f(type, fn, capture);
    }

    EventTarget.prototype.removeEventListener = function(type, fn, capture) {
        this.f = f_remove;
        if(type == 'scroll' && typeof _ === 'function')
            fn = _.throttle(fn, 350);
        this.f(type, fn, capture);
    }

It overwrites the prototype for add/removeEventListener -> And if the event is a scroll event, it surrounds the Function fn with _.throttle().

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda