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
// ...
}