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

69
Views
React Native how to run function 3 times then check for conditions

I have a process in my useEffect Hook. I need this process to run 3 time then stop. Also Every time the variable getData (data string from api) changes I need the process to repeat (meaning to run 3 time). The current code works some times and some it keeps going without stopping. Even when getData does not change. It's seem as my if statement is not responding. I don't see console.log('should stop') print. I have checked to see if the value in the if statement is a number and it is. It's just not running the condition

// getData is string that will change from api

// use to stop the setInterval 
const getImageCounter = useRef();

useEffect(() => {
    let source = axios.CancelToken.source();
    let imageCount = 0;

    getImageCounter.current = setInterval(() => {
      if (imageCount >= 3) {
        // stop setInterval when conditions are meet
        clearInterval(getImageCounter.current);
        console.info('should stop');
      }

      imageCount++;

      console.log('rendering', imageCount);
      axios
        .get(`${network}/Image`, {
          cancelToken: source.token,
        })
        .then(imageResponse => {
          const imageFilter = imageResponse.data.icon.substring(0, imageResponse.data.icon.lastIndexOf('=') + 1); //prettier-ignore
          setBase64Image(imageFilter);
        })
        .catch(function (e) {
          if (axios.isCancel(e)) {
            console.log(`request cancelled:${e.message}`);
          } else {
            console.log(
              'another error happened fetching overlays:' + e.message,
            );
          }
        });
    }, 3000);

    return function () {
      source.cancel('Cancelling in cleanup');
    };

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [getData]);
about 4 years ago · Juan Pablo Isaza
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!