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

155
Views
Next Run code until Button is clicked again

learning next and stuff and had a question I did not have any success on finding online.

So I have a button

<button onClick={toggle} className="common-btn">{buttonText}</button>

I am wanting this button to control how long a section of code is ran. So within the toggle method I update the state and that works just fine. I also run a while loop hoping that if the state ever changed it would break out of the while loop running the code like so

...
setRunning(!running)
if(running){
    while(running) 
    {
      console.log("Running")
    }
}

... 

The issue comes in since it never breaks out of the while loop so even if the state is false it will continue to run the original while loop. Does anyone have any better insight on how to go about this?

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

0

<button onClick={() =>toggle() } className="common-btn">{buttonText}</button>
setRunning(!running)
if(running){
     While(running) {
      console.log("Running")
      } 
}else{
 console.log("Not Running) 
} 

Wyh dont you type like this?

about 4 years ago · Juan Pablo Isaza Report

0

 const [running, setRunning] = useState(true) 
const toggle = () => {
  setRunning(!running) 
} 
<button onClick={() =>toggle() } className="common-btn">{buttonText}</button>
    if(running){
          console.log("Running")
    }else{
     console.log("Not Running) 
    } 
Wyh dont you type like this? 
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!