最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - web worker postMessage process order - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 7

Yes 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.

发布评论

评论列表(0)

  1. 暂无评论