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

305
Visualizações
How do i stop a specific function in JavaScript? I dont want the whole program to stop
client.on('message', msg => {
  if (msg.content === 'startspam') {
    function spam(){
      msg.channel.send('a')
    }
    setInterval(spam, 2500)
  }
});

client.on('message', msg => {
  if (msg.content === 'stopspam') {
    
  }
});

how to stop the function spam ( sorry I'm new to JavaScript and im trying to learn the basics )

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

0

You can use the clearInterval function on the number that setInterval returns to stop the interval. I added some code below to show how that works.

var interval = setInterval(someFuntion, 200);
clearInterval(interval);
about 4 years ago · Juan Pablo Isaza Relatório

0

Try clearInterval

const print = () => console.log('alive');
const intervalId = setInterval(print, 1000);
const stopInterval = () => clearInterval(intervalId);
<button onclick='stopInterval()'>Stop</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

first of all java and javascript are not the same..

and for your question -

you can use this clearInterval().

this function receives the id of the interval you want to stop. and the interval Function returns a unique number so it can used as ID..

the way you do it is somthing like this :

var intervalID = '';

function startInterVal() {
  intervalID = setInterval(() => {
    console.log('Hello')
  }, 1000)
}

function stopInterval() {
  clearInterval(intervalID);
}
<button onClick="startInterVal()">Start</button>

<button onClick="stopInterval()">Stop</button>

hope i helped you :)

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