I know you will all be going like "use timers!" but let me explain. I'm making something like macro and I need functions to be executed with a specific delay. I know that it can be done with timers, but how would a code look with 50 timers and 50 callbacks for them? With sleep it could be all nice in one function. Since web workers run on separate thread, there is no problem of freezing.
I only know 2 methods of making sleep without eating cpu:
- Make a synchronous xhr request to a special backend that sleeps for x amount of seconds given by GET. While this works it's really inconvenient and depends on ping to the server.
- Use generator functions (yield) and send a message to main thread which starts a timer. When timer executes, message is sent back to the worker to continue execution. This is a very good solution but it's only available in firefox as I know. Chrome has no support of generator functions.
Are there any other ways of achieving sleep function?