0

Using useState and setTimeout, I am trying to run one timer for 4 seconds, and a second one for an additional 4 seconds (totaling 8 seconds on the second one). But when I use 2 setTimeOut's in a single react control, I get a strange behavior where it appears the second setTimeout is looping itself.

I've tried:

setTimeout(()=>{
   setReadinessCheckStatus('Checking');
}, 4000);

setTimeout(()=>{
   setReadinessCheckStatus('Checked');
}, 8000);

following other documentation I found on stack overflow (Effective way to running multiple setTimeout at the same time in React Native)

let loop1 = setTimeout(()=>{
   setReadinessCheckStatus('Checking');
}, 4000);

let loop2 = setTimeout(()=>{
   setReadinessCheckStatus('Checked');
}, 8000);

and even nested loops.

let loop1 = setTimeout(()=>{
   setReadinessCheckStatus('Checking');

   setTimeout(()=>{
        setReadinessCheckStatus('Checked');
   }, 2000);
}, 4000);

JackieG
  • 1
  • 2

1 Answers1

0

You should show all your component code. Did you write setTimeout in useEffect?

Zotille
  • 113
  • 8