Suspend js script until event occurs

Is there a way to pause the js script until some event occurs, for example, clicking a button? I mean, the script should pause its execution and resume again when the event occurs. I am partly new to javascript, and even after a thorough search on the net, I could not find a solution!

0
source share
3 answers

JS is single-threaded, and multithreading can only be done for web workers. As far as I know.

However, if you have only a small script that you want to pause, you can have a global variable / flag and just in your script (I somehow assume that this is a function of a loop or event) there is a check for this flag. In this case:

var flag = false; ... if (flag) { do your code } else { do nothing or return if in function } 

As soon as you want to continue, just set flag = true;

+1
source

JavaScript is single-threaded without any attempt when events occur. It's impossible.

0
source

If what you are trying to pause is a function that would otherwise save a loop, I came up with a good solution: fooobar.com/questions/914423 / ...

0
source

Source: https://habr.com/ru/post/1275665/


All Articles