0

I understand that JavaScript is a single-threaded language but I was wondering if there is any way to stop the execution of a long task if it`s taking too long.

I am working on a calculator and I use a lot of 3rd party libraries to perform calculations. Sometimes calculations can take too long and I want to be able to time-out, stop the calculation, and show an error or something.

So far I discovered that I can achieve this with WebWorkers but I don`t really like using them and they also introduce some overhead.

Is there any better way of achieving this?

Saik
  • 993
  • 1
  • 16
  • 40
  • 2
    Does this answer your question? [How to implement a "function timeout" in Javascript - not just the 'setTimeout'](https://stackoverflow.com/questions/8778718/how-to-implement-a-function-timeout-in-javascript-not-just-the-settimeout) – jmargolisvt Sep 07 '20 at 17:25
  • No :( Unfortunately none of these actually provide a good way of timing out a function. – Saik Sep 07 '20 at 17:53
  • What are you defining as "good" here? Specifically, why don't these answers work for you? – jmargolisvt Sep 07 '20 at 17:55
  • I tried it with a function that has a while loop that loops for 10 seconds for example, and besides the worker's solution (I already know about this and I am trying to avoid using it), the rest of the solutions are not able to terminate the function. – Saik Sep 07 '20 at 18:01

0 Answers0