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

153
Visualizações
throttle function broke my input live search

I am trying to throttle my html on input when user enters some words into search field there must be a redrawing of the block. When I implemented throttle function into my code, the live search stopped working, now the card redrawing does not happen

    searchInput.addEventListener('input', (event: Event) => {
  searchTerm = (event.target as HTMLTextAreaElement).value.toLowerCase();
  throttle(() => {
    showList();
  }, 100);
});

function throttle(func: Function, ms: number) {
  let isThrottled: boolean = false;
  let savedArgs: any, savedThis: any;

  function wrapper(this: any) {
    if (isThrottled) {
      savedArgs = arguments;
      savedThis = this;
      return;
    }
    func.apply(this, arguments);
    isThrottled = true;

    setTimeout(() => {
      isThrottled = false; // (3)
      if (savedArgs) {
        wrapper.apply(savedThis, savedArgs);
        savedArgs = savedThis = null;
      }
    }, ms);
  }

  return wrapper;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your throttle returns a function. You called throttle() but you didn't use the function it returns.

You should do something like that

  const throttledShowList = throttle(showList, 100); // <-- Add this
  searchInput.addEventListener('input', (event: Event) => {
  searchTerm = (event.target as HTMLTextAreaElement).value.toLowerCase();
  throttledShowList(); // <-- Replace the previous throttle() call with this
});

function throttle(func: Function, ms: number) {
  let isThrottled: boolean = false;
  let savedArgs: any, savedThis: any;

  function wrapper(this: any) {
    if (isThrottled) {
      savedArgs = arguments;
      savedThis = this;
      return;
    }
    func.apply(this, arguments);
    isThrottled = true;

    setTimeout(() => {
      isThrottled = false; // (3)
      if (savedArgs) {
        wrapper.apply(savedThis, savedArgs);
        savedArgs = savedThis = null;
      }
    }, ms);
  }

  return wrapper;
}

That way, you define a throttled version of your function that you call on input

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