0

When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. Thus I want to use workers. I have a problem:

<html>
<head>
</head>
<body>
<button onclick="startWorker()">Start Worker</button> 
<button onclick="stopWorker()">Stop Worker</button>
<br><br>

<script>
    var w;

    function startWorker()
    {
        if (typeof (Worker) !== "undefined")
        {
            if (typeof (w) == "undefined")
            {
               // Do something
            }
        }
        else
        {
            alert("Browser does not support Web Workers.");
        }
    }

    function stopWorker()
    {
        w.terminate();
        w = undefined;
    }
</script>
</body>
</html>

I get the "Browser does not support Web Workers." message.

Is there other way to use multithreading so the page becomes responsive before the script is finished?

Thanks a lot, Orian.

Orian Zinger
  • 87
  • 1
  • 2
  • 11
  • http://stackoverflow.com/questions/9516900/how-can-i-create-an-asynchronous-function-on-javascript – didierc Dec 30 '14 at 20:18
  • You might also be interested in frp libraries like kefir: http://pozadi.github.io/kefir/ – didierc Dec 30 '14 at 20:22
  • But if you want *true* multithreading, I'm afraid the only solution is Web workers. – didierc Dec 30 '14 at 20:23
  • 1
    @didierc even if you defer the execution via `setTimeout`, this still does not prevent the function from blocking once its executed. You could break the function into multiple `setTimeout`s but that can sometimes be impractical. – levi Dec 30 '14 at 20:34
  • @didierc, as levi said - setTimeoutstill does not prevent the function from blocking once its executed. thanks anyway :) – Orian Zinger Dec 30 '14 at 21:24
  • That was a knee jerk reaction to the more general question: "how to do multithreading in js?". As I said later on, for **true** multithreading I don't think there's an alternative to `Worker`s. This being said, if you reformulate your question as: how do I **change** my code so that it becomes responsive while seemingly handling several tasks, the other question and its answers still applies. – didierc Dec 30 '14 at 23:02

1 Answers1

1

If the scripts make your page unresponsive, they're poorly programmed. Look into asynchronous programming - after all, JS has worked for many years without workers.

That said, if you really need web workers, you essentially have some choices:

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • To be fair, the tasks used for JS now are very different from those in the past (eg: pixel-level processing). – levi Dec 30 '14 at 20:38
  • offload the work into C# side sound good to me. Thanks a lot! – Orian Zinger Dec 30 '14 at 21:25
  • The C# solution is not for browser without workers, it's for IE. Is IE the only browser not having support for workers? – didierc Dec 30 '14 at 23:19
  • I have IE11 and it does support workers, but in my WPF project it doesn't. – Orian Zinger Dec 31 '14 at 10:20
  • @OrianZinger; that's because WPF uses IE7 by default and IE7 does not support web workers. If you force WPF to use IE11(see link in my answer), it will work - I've tested. – Erti-Chris Eelmaa Dec 31 '14 at 23:38
  • I can't use IE11 in my WPF, so I forced it to use IE10 and still i get the same result. IE10 should support web workers. When I forced it to use IE11 it worked, but my Google Earth Plugin didn't like it. I had to download the plugin again and even though - it didn't work. – Orian Zinger Jan 01 '15 at 08:37