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

208
Views
How can I stop setInterval?

I'm trying to clear setInterval with calling clearInterval. however, i call setInterval function inside function. So I'd like to trigger the parameter in the setInterval by clearInterval, But i do not know how to get reach this thing. And what i say is confusing so I just want to show u the code :

function getAPI() {
    const polling = () => {
      if (convert !== undefined) {
        axios
          .post("/realtime", {
            GET_API_URL: convert.GET_API_URL,
            Headers1_key: convert.Headers1_key,
            Headers1_Value: convert.Headers1_Value,
            Headers2_key: convert.Headers2_key,
            Headers2_Value: convert.Headers2_Value,
            Headers3_key: convert.Headers3_key,
            Headers3_Value: convert.Headers3_Value,
          })
          .then(response => {
            setRandom(response.data);
          })
          .catch(error => {
            console.log(error);
          });
      } else {
        alert("try again.");
      }
    };
    setInterval(polling, convert.API_Read_Speed);
  }

  const init = () => {
    clearInterval(polling);  // i want to call polling and i want to stop the axios posting
  };

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

according to your comment seems like you need to stop the polling in react. you can use the useRef hook to keep the reference of the interval and clear it whenever you like.

 useEffect(() => {
  interval.current = setInterval(() => {
    ...
  }, ms)

 }, [input]);

 const closeConnection = () => {
  clearInterval(interval.current);

 }
about 4 years ago · Juan Pablo Isaza Report

0

You can store setInterval inside a variable and access it like that.
Example:

let interval;

function foo() {
   interval = setInterval(() => {
      ...
   }, ms)
}

function bar() {
   clearInterval(interval);
}
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!