admin管理员组文章数量:1025218
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
1 Answer
Reset to default 3It 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.
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
1 Answer
Reset to default 3It 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.
本文标签: javascriptTypeError Not enough arguments to WindowpostMessageStack Overflow
版权声明:本文标题:javascript - TypeError: Not enough arguments to Window.postMessage - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745615491a2159265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论