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

148
Visualizações
Why debouce function prints the value differently in textbox?

Problem: In the code below, I tried to type "red" continuously and after 2 seconds, "red" appeared in the textbox. Then, after a few seconds, I tried to type "re" continuously. Now, after 2 seconds I got "STACK OVERFLOW DOES NOT ALLOW ME PROVIDE OUTPUT HERE".

Please try it yourself.


    import React from 'react';
    import { useCallback, useEffect, useState } from 'react';
    import './style.css';
    
    export default function App() {
      const [inputValue, setInputValue] = useState('');
    
      
    
    function debouce(fn) {
        let timeout;
      return function() {
        console.log("final", arguments[0])
        clearTimeout(timeout);
        timeout = setTimeout(() => fn.apply(this, arguments), 1000);
      };
      }
    
      const getvalue = debouce(setInputValue);
    let a = "";
      const handleChange = useCallback((e) => {
        const value = e.target.value;
        a = a+ value;
        console.log('targetvalue', a);
        getvalue(a);
      }, []);
    
      return (
        <div>
          <input onChange={handleChange} value={inputValue} />
          <h1>Hello StackBlitz!</h1>
          <p>Start editing to see some magic happen :)</p>
        </div>
      );
    }

Demo link: https://stackblitz.com/edit/react-nh3ty2

Why? Can anyone explain please how to do it correctly as well

UPDATE: i understood that debouce decorator cannot be altered for the required criteria so im changing from below code

function debouce(fn) {
    let a = '',
      timerid;
    return function (e) {
      a = a + e;
      clearTimeout(timerid);
      timerid = setTimeout(function () {
        fn.apply(this, [a]);
        a = '';
      }, 2000);
    };
  }

to this

function debouce(fn) {
    let timeout;
  return function() {
    clearTimeout(timeout);
    timeout = setTimeout(() => func.apply(this, arguments), ms);
  };
  }
about 4 years ago · Juan Pablo Isaza
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