Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda