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

117
Views
Stop function as soon as variable is false

Note:
The actual function works with async. During the code execution await is used several times. The execution of the actual function takes about 20 seconds. The actual function is here on GitHub.

The idea:
The function func is executed as soon as variable specVar is True. If at any time during this execution specVar becomes False, the whole function should be "aborted" or stopped.

The problem:
I already tried to put the function into a while loop (while(specVar{}) of the function continues to be executed, even if the while loop should actually stop (specVar = False).
Now I put an if query before every single "function point" to check if specVar is still True. However, the more complex the function becomes, the more effort it takes to include this query.

The current code

let specVar

document.getElementById("funcStart").onclick = () => {
    specVar = true
    func()
}
document.getElementById("funcStop").onclick = () => { specVar = false }

function func() {
    console.log("Function starts")
    // Some code...
    if (specVar) console.log("Function still running, 1")
    else return
    // Some code...
    
    if (specVar) console.log("Function still running, 2")
    else return
    // ...
}
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!