Since web workers negate the disadvantages of paused execution in JavaScript, as they will not block UI, is there a reliable method to get them to Sleep?
Either for a designated time, or until they receive a message?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Webworkersare essentially functions that are called viaonMessage, so getting them to ‘sleep’ should simply be a matter of sending a specific message to at after a certain period of time.Unlike threads in languages like Java,
WebWorkersneed not operate in a continuous (and CPU eating) loop in order to retain usefulness; they can be messaged as many times as you please.In this example, the worker ‘sleeps’ as it waits for user input.
index.html:
worker.js:
Forgive me if I misread your question.
Edit:
Another possibility is, assuming the
WebWorkeris running in asetTimeoutloop, listen for a message that could callclearTimeoutwhile a user is doing something.Update:
This code will create an object, modify it via the worker, and then modify it again after the parent page adjusts a flag:
index.html:
worker.js: