admin管理员组文章数量:1393117
With the following code:
HTML FILE:
<!DOCTYPE html>
<html>
<head>
<title></title>
<!--<script type="text/javascript" src="worker.js"></script>-->
<script type="text/javascript" src="run.js"></script>
</head>
<body>
<div id='log'></div>
</body>
</html>
run.js:
window.onload = function(){
var worker = new Worker('worker.js');
var log = document.getElementById('log');
log.innerHTML += "<p>"+"test"+"</p>";
worker.addEventListener('message', function(e) {
//alert(e.data);
log.innerHTML += '<p>' + e.data + '</p>';
}, false);
for(var i=1; i<21;i++){
worker.postMessage(i);
}
};
worker.js
self.addEventListener('message', function(e) {
self.postMessage(e.data);
}, false);
the output is as I would expect a list of 1 to 20, but if I unment the alert call in the run.js message listener it prints out 20 to 1 instead. Is this because of the alert causing the delay in writing to the page and the message processing is backed up in a stack so the last one on is the first one off? or is it something else.
With the following code:
HTML FILE:
<!DOCTYPE html>
<html>
<head>
<title></title>
<!--<script type="text/javascript" src="worker.js"></script>-->
<script type="text/javascript" src="run.js"></script>
</head>
<body>
<div id='log'></div>
</body>
</html>
run.js:
window.onload = function(){
var worker = new Worker('worker.js');
var log = document.getElementById('log');
log.innerHTML += "<p>"+"test"+"</p>";
worker.addEventListener('message', function(e) {
//alert(e.data);
log.innerHTML += '<p>' + e.data + '</p>';
}, false);
for(var i=1; i<21;i++){
worker.postMessage(i);
}
};
worker.js
self.addEventListener('message', function(e) {
self.postMessage(e.data);
}, false);
the output is as I would expect a list of 1 to 20, but if I unment the alert call in the run.js message listener it prints out 20 to 1 instead. Is this because of the alert causing the delay in writing to the page and the message processing is backed up in a stack so the last one on is the first one off? or is it something else.
Share Improve this question asked May 4, 2012 at 14:04 Daniel RobinsonDaniel Robinson 14.9k20 gold badges66 silver badges116 bronze badges 1- Fun fact: In Firefox I see 20, 19, 18, ... as described by you. In Chrome it is 1, 2, 3, ... – h0b0 Commented Jan 29, 2013 at 14:04
2 Answers
Reset to default 7Yes this is because of "alert()". It blocks the further execution of the code inside the block of the worker listener. By other words, the code:
log.innerHTML += '<p>' + e.data + '</p>';
is executed only after the "OK" button on the modal window of the alert box is pressed, in wich order you will press them, in this order the "log.innerHTML" will be changed, so you press them in descending order and that's why you get this result. If you don't use alert messages everything goes well.
I think this was a bug in Firefox. I'm not seeing this behavior in 2020 in any browser I tried: they all order from 1 to 20.
本文标签: javascriptweb worker postMessage process orderStack Overflow
版权声明:本文标题:javascript - web worker postMessage process order - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744632375a2616618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论