Hi I have a Javascript function that does some animations on button click and has some sleeps inside, here is pseudocod:
const sleep = ms => new Promise(s => setTimeout(s, ms));
async function f(data) {
...
while( arg ) {
doAnimation
await sleep(200)
}
}
My problem is, when button is pressed second time before the end of the animation, weird staff happends - I have two animations going simultanoiusly.
My question is, what is the best way to stop this finction on button press and recall it. I thought about a flag, but I have trouble writing it in a way, so the running function would react to it and new called not.