public
Last active

Unstoppable Web Worker

  • Download Gist
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<!DOCTYPE>
<html>
<head></head>
<body>
<p>Start the worker from the browser console (fire up a CPU monitor to
have some sort of visual feedback)</p>
<pre>worker.postMessage("run")</pre>
<p>you will NOT be able to stop it with</p>
<pre>worker.postMessage("stop")</pre>
<p>What I'd expected:</p>
<pre>worker.postMessage("run") // start
worker.postMessage("stop") // stop
worker.postMessage("run") // start from where it was interrupted</pre>
 
<script type="text/javascript">
var worker = new Worker("worker.js");
</script>
</body>
</html>
worker.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
var count = 0,
running = false,
iface = {};
 
 
iface.run = function ()
{
running = true;
while (running) {
count++;
}
};
 
 
iface.stop = function ()
{
running = false;
};
 
 
onmessage = function (ev)
{
var func_name = ev.data;
iface[func_name].apply();
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.