Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!