Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:54:10+00:00 2026-05-29T10:54:10+00:00

Since web workers negate the disadvantages of paused execution in JavaScript, as they will

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:54:11+00:00Added an answer on May 29, 2026 at 10:54 am

    Webworkers are essentially functions that are called via onMessage, 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, WebWorkers need 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:

    <script>
        var worker = new Worker("worker.js");
    
        worker.onmessage = function(e) {
            console.log("Send and recieved " + e.data);
        }
    
        function tellWorker(element) {
            var data = element.value;
            worker.postMessage(data);
        }
    
    </script>
    

    worker.js:

    self.onmessage = function(e) {
        self.postMessage(e.data);
    }
    

    Forgive me if I misread your question.

    Edit:
    Another possibility is, assuming the WebWorker is running in a setTimeout loop, listen for a message that could call clearTimeout while 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:

    <input type="button" value="start" onclick="startWork()" /><br>
    <input type="button" value="passObject" onclick="finishWork()" />
    
    <script>
        var worker = new Worker("worker.js");
    
        var incompleteObject = {
            val     : 0,
            done    : false
        };
    
    
        worker.onmessage = function(e) {
            console.log("Exit Status: ");
            console.log(e.data);
        }
    
        function startWork() {
            worker.postMessage(incompleteObject);
        }
        function finishWork() {
            incompleteObject.done = true;
            worker.postMessage(incompleteObject);
        }
    
    </script>
    

    worker.js:

    self.onmessage = function(e) {
    
        var obj = e.data;
    
        if(obj.done == false) {
            obj.val = 2;
        } else {
            obj.val = 4;
        }
    
        self.postMessage(obj);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So since System.Web & HttpContext isn't available in Silverlight is there a way to
Hope this question is not stupid since I am an amateur web designer. I
I'm developing a web application that will interact with google APIs. Since the only
This is about a web app that serves images. Since the same request will
What solutions are available to access Profile information since Web Applications do not expose
Today I was introduced to the world of Web Workers in JavaScript. This made
Is there any way for me to share a variable between two web workers?
I know this is up to google, but since the web has evolved then
Since the format of the response of a web service request seems to be
Since a month ago i am studying restful web services really hard. Now that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.