javascript - TypeError: Not enough arguments to Window.postMessage - Stack Overflow

I am learning about the web worker. I am using the follow tutorialSo far, it works. I have the followin

I am learning about the web worker. I am using the follow tutorial

So far, it works. I have the following code

var worker = new Worker('thing.js');

worker.addEventListener('message', function(e) {
    alert("Worker said: " +  e.data);
}, false);

worker.postMessage("Test me");

and in my thing.js file

self.addEventListener('message', function(e) {
   self.postMessage(e.data);
}, false);

The above works fine.

However, I need to pass a message from within my thing.js back to my main js file, to demonstrate it passing back a progress update.

Again, the tutorial shows I can do this, so I have the following updated code

var worker = new Worker('thing.js');

worker.addEventListener('message', function(e) {
    alert("Worker said: " +  e.data);
}, false);

worker.postMessage("Test me");  

and in my thing.js

self.addEventListener('message', function(e) {
  self.postMessage(e.data);
}, false);

function DoThisThing() {
    postMessage("I should ALSO be working but I am not.");
}

This fails with an error message (in FireBug):

TypeError: Not enough arguments to Window.postMessage.

I can't see what I've done wrong.

I am learning about the web worker. I am using the follow tutorial

https://developer.mozilla/en/docs/Web/Guide/Performance/Using_web_workers

So far, it works. I have the following code

var worker = new Worker('thing.js');

worker.addEventListener('message', function(e) {
    alert("Worker said: " +  e.data);
}, false);

worker.postMessage("Test me");

and in my thing.js file

self.addEventListener('message', function(e) {
   self.postMessage(e.data);
}, false);

The above works fine.

However, I need to pass a message from within my thing.js back to my main js file, to demonstrate it passing back a progress update.

Again, the tutorial shows I can do this, so I have the following updated code

var worker = new Worker('thing.js');

worker.addEventListener('message', function(e) {
    alert("Worker said: " +  e.data);
}, false);

worker.postMessage("Test me");  

and in my thing.js

self.addEventListener('message', function(e) {
  self.postMessage(e.data);
}, false);

function DoThisThing() {
    postMessage("I should ALSO be working but I am not.");
}

This fails with an error message (in FireBug):

TypeError: Not enough arguments to Window.postMessage.

I can't see what I've done wrong.

Share Improve this question edited Sep 29, 2014 at 14:14 MyDaftQuestions asked Sep 29, 2014 at 12:33 MyDaftQuestionsMyDaftQuestions 4,73117 gold badges75 silver badges140 bronze badges 9
  • You mented out the window message that triggers the worker's message, which seems to just echo the original message. – kennebec Commented Sep 29, 2014 at 12:59
  • try adding target origin: developer.mozilla/en-US/docs/Web/API/Window.postMessage – akonsu Commented Sep 29, 2014 at 14:18
  • @kennebec, apologies, that was a mistake in my post, it's now corrected – MyDaftQuestions Commented Sep 29, 2014 at 14:26
  • @akonsu, how words though? What is the origin? This site is running locally, so I can't use www. or localhost. and I'm not alllowed to do c:\ – MyDaftQuestions Commented Sep 29, 2014 at 14:26
  • maybe '*' for anything. it must have an origin. running script from a local file without a web server is inadequate. – akonsu Commented Sep 29, 2014 at 14:33
 |  Show 4 more ments

1 Answer 1

Reset to default 3

It would be convenient to have the full source code that triggered the issue, but I bet the problem was that besides instantiating the worker your code was also including the worker file via a script tag. Something like:

<script type="text/javascript" src="thing.js"></script>

So basically what you have is:

thing.js (worker)

   self.addEventListener('message', function(e) {
      self.postMessage(e.data);
    }, false);

    function DoThisThing() {
        postMessage("I should ALSO be working but I am not.");
    }

    DoThisThing();

I assume you were calling DoThisThing() because otherwise you won't get the "TypeError: Not enough arguments to Window.postMessage" error.

If you run the code as it is it works, but you're also seeing the error.

TypeError: Not enough arguments to Window.postMessage.
Worker said: I should ALSO be working but I am not.  
Worker said: Test me

So what was triggering the TypeError: Not enough arguments...? If you were sourcing the file from a script tag, the function gets executed two times actually. On the first call, there's an error.

When thing.js gets sourced by the script tag, DoThisThing() gets executed. That's the first call. DoThisThing() calls postmessage (same as self.postmessage). But this call gets resolved to window.postmessage. This method requires a second argument (targetOrigin).

However when the worker gets executed, the call to postmessage (same as self.postmessage) gets resolved to DedicatedWorkerGlobalScope.postmessage. This method doesn't require a second argument.

When you added a second argument to postmessage, you solved the issue for the first call but not the second. Thus you got a new error: "TypeError: Argument 2 of DedicatedWorkerGlobalScope.postMessage can't be converted to a sequence".

Summarizing, there are two types of postmessages, one that belongs to the window object and another one that belongs to the DedicatedWorkerGlobalScope object. A simple call to postmessage gets normally resolved by the DedicatedWorkerGlobalScope object. This call is convenient to municate workers with their parent. window.postmessage requires a second parameter specifying the targetOrigin. This call is convenient for cross-origin munications.

In your case, the likely cause of the issue was sourcing the worker JavaScript file from a script tag. That caused postmessage to get resolved by window, which required a second argument.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745615492a4636210.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信