+ 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 đ
3 Answers
+ 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
+ 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
+ 2
Thanks