I have tried to use importScripts to load a second JavaScript file into my web worker, but although no error occurred, it didn't work. I narrowed the problem down to this very simple situation:
In the main HTML file:
<script>
var w = new Worker(script1.js);
w.addEventListener(message, function(e){
alert(e.data);
})
w.postMessage();
</script>
In script1.js:
self.addEventListener(message, function(e){
var a = 5;
importScripts(script2.js);
self.postMessage(a);
})
In script2.js:
a = 6
I would like to see a dialog displaying 6, because a was changed from 5 to 6 by importing script2.js, but the dialog shows 5. What am I missing here?