Reputation: 159
Is there any way to suspend a js script until some event like a mouse click on a button occurs?? I mean the script should pause its execution and resume again when the event occurs. I am kind of new to Javascript and even after a thorough search on the net couldn't find a solution!!
Upvotes: 0
Views: 2351
Reputation: 10579
If what you are trying to pause is a function which would otherwise keep looping, I've come up with a good solution: https://stackoverflow.com/a/47841425/2884291
Upvotes: 0
Reputation: 13597
JS is single threaded and multithreading can be done us Web Workers only. As far as I know.
However if you have only a little script which you want to suspend, you can have a global variable/flag and simply in your script(I somehow believe it's a loop or event driven function) have a check for that flag. In this case:
var flag = false;
...
if (flag) {
do your code
} else {
do nothing or return if in function
}
Once you want to continue, just set flag = true;
Upvotes: 1
Reputation: 74204
JavaScript is single threaded with no preemption when events occur. It's not possible.
Upvotes: 0