+ 7

[SOLVED] How can I stop a function when it is called I'd the function is already running? (JS)

I want to create a function which, when called, if it's already running it stops and if it is stopped then it runs. How to create such a function? Please answer. Thanks in advance 😇 Happy Coding 👍

30th Jul 2021, 6:19 PM
Adil
Adil - avatar
3 ответов
+ 5
Assuming you are running a timer process, then you could use setTimeout to control operation. Use a function to trigger a running status, the timer function can check for running status at the end of the function, before next setTimeout is triggered. https://code.sololearn.com/WKzM5u7EvE05/?ref=app
31st Jul 2021, 2:07 AM
Calviղ
Calviղ - avatar
+ 6
according to my knowledge JavaScript is single threaded so you cant have multiple functions running at the same time ( you could use events in the case : https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events ) but then also you may want to use service workers if you really want to run a background task . running a long running task on main thread would crash the browser
30th Jul 2021, 6:39 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
Thanks
31st Jul 2021, 5:12 AM
Adil
Adil - avatar