JavaScript method abort

How can I interrupt the execution of the JavaScript method? The method is planned using the "setTimeout" function in JavaScript, I need to interrupt the execution of this method (without calling the clearTimeout function), because it cannot interrupt the execution of the method while it is running, but it can cancel before it has already started, after started the execution of the method "clearTimeout" does not interrupt the execution of the method). I want to interrupt when a certain event has occurred and take control of the flow. What is the way to work with javascript?

+3
source share
2 answers

If your method is synchronous, you cannot. If it performs asynchronous operations, you should use a flag that each operation checks before starting.

Just like SleepyCod, you can use setInterval / clearInterval, but you can only do this if your method needs to do the same at certain times.

+3
source

Do not use setTimeout, but setInterval, and you can cancel execution with

clearInterval({your interval});
+2
source

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


All Articles