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

220
Visualizações
Why setInterval only works fine at first time?

I am trying setInterval in js to write 'is typing' when someone types but it only works fine at first time after that each time takes less time to do the function!

var personTyping = document.getElementById('typingArea');
var isTypingPlace = document.getElementById('isTypingPlace');
personTyping.addEventListener('keypress', () => {
  isTypingPlace.innerHTML = "درحال نوشتن...";
})
personTyping.addEventListener('keyup', () => {
  setInterval(() => {
    isTypingPlace.innerHTML = null;
  }, 1000)
})
<html lang="en" dir="rtl">

<div id="typingArea"><input type="text"></div>
<p id="isTypingPlace"></p>

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

0

Everytime you create an Interval with setInterval() it will keep triggering the callback forever until you delete the interval again. When your event listener gets called you create new intervals which will all continue to trigger. So you should delete old intervals with clearInterval() before creating a new one.

var personTyping = document.getElementById('typingArea');
var isTypingPlace = document.getElementById('isTypingPlace');
personTyping.addEventListener('keypress', () => {
  isTypingPlace.innerHTML = "درحال نوشتن...";
})

let interval = null
personTyping.addEventListener('keyup', () => {
  if (interval) clearInterval(interval);
  interval = setInterval(() => {
    isTypingPlace.innerHTML = null;
  }, 1000)
})
<html lang="en" dir="rtl">

<div id="typingArea"><input type="text"></div>
<p id="isTypingPlace"></p>

about 4 years ago · Juan Pablo Isaza Relatório

0

I think that there is a major problem with the logic. You want to show the result for 1 second and then clear the content. But every keyup event, you are creating a new interval which I believe is not the intended behavior. You can use setTimeout instead, and can use clearTimeout which acts like a debounce effect for 1 sec.

var personTyping = document.getElementById('typingArea');
var isTypingPlace = document.getElementById('isTypingPlace');

let timeoutRef;

personTyping.addEventListener('keypress', () => {
  isTypingPlace.innerHTML = "درحال نوشتن...";
})
personTyping.addEventListener('keyup', () => {
  clearTimeout(timeoutRef)
  timeoutRef = setTimeout(() => {

    isTypingPlace.innerHTML = null;
    console.log('Content cleared');

  }, 1000)
})
<div id="typingArea"><input type="text"></div>
<p id="isTypingPlace"></p>

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you want to use setTimeout instead of setInterval

var personTyping = document.getElementById('typingArea');
var isTypingPlace = document.getElementById('isTypingPlace');
personTyping.addEventListener('keypress', () => {
  isTypingPlace.innerHTML = "درحال نوشتن...";
})
personTyping.addEventListener('keyup', () => {
  setTimeout(() => {
    isTypingPlace.innerHTML = null;
  }, 1000)
})
<html lang="en" dir="rtl">

<div id="typingArea"><input type="text"></div>
<p id="isTypingPlace"></p>

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