【问题标题】:web workers behave differently in webkit than Firefoxweb worker 在 webkit 中的行为与 Firefox 不同
【发布时间】:2011-12-16 23:01:31
【问题描述】:

我有一个在现代基于 webkit 的浏览器中运行良好的 Web 应用程序 (http://montecarlo-tester.appspot.com/)。基本上,它使用 webworker 从服务器获取数据,然后在执行一些计算后将其发回。

它在 Chrome/Safari 中运行良好(控制台中没有错误),但是当我尝试在 Firefox 中使用它时,它就不行了。我推断出在 Firefox 中变量“迭代”的设置不正确。不幸的是,Firefox 缺少调试器(适用于网络工作者),而 javascript 具有功能范围,因此很难确定问题出在哪里。我已经为我的网络工作者发布了 javascript 代码,我想知道是否有人能指出我哪里出错了:

importScripts('/static/js/mylibs/jquery.hive.pollen-mod.js');


$(function (data) {
    main();
    //while(main());
    close();
});

function main() {
    //make an ajax call to get a param
    var iterations//value will be set by server response
    var key//key of the datastore object
    var continueloop = true;
    p.ajax.post({
        url:'/getdataurl',
        dataType: "json",
        success: function(responseText){
            if (responseText === null) {
                var workermessage = {
                    "log":"responseText is null. Either the server has issues or we have run out of stuff to compute."
                };
                $.send(workermessage);
                continueloop = false;
            }
            iterations = responseText.iterationsjob;
            key = responseText.key;      
        }
    });

    if (continueloop === false) {
        return false;
    }

//here is where I think the problems begin. In chrome/safari, iterations = 1000. 
//In Firefox however, iterations = null. As a result, everything after that does not work.

    var i,x,y,z;
    var count = 0;
    var pi;
    start = new Date();
    for (i=0;i<iterations;i++) {
        x = Math.random();
        y = Math.random();
        z = x*x+y*y;
        if(z<=1.0){
            count++;
        }
    }//end for loop
    pi = count/(iterations)*4.0;
    end = new Date();
    result = {
        "estimated_pi":pi,
        "num_iter":iterations,
        "duration_ms":end.valueOf()-start.valueOf(),
        "key":key
    };
    //send results to the server
    p.ajax.post({
        url: "/resultshandler",
        dataType:'json',
        data: result,
        success: function()
        {
            //do nothing!
        }
    });
    $.send(result);
    return true;//persists the loop
}

【问题讨论】:

  • "很遗憾,Firefox 缺少调试器" 你试过Firebug吗?
  • var iterationsvar key 的注释之前的变量名后面加一个分号有区别吗?
  • 您的p.ajax.post 函数是在做异步ajax 还是同步ajax?如果是异步的,那么 iterations 只会在您的成功处理程序中设置,而不是在您要求设置它的位置,因为 ajax 调用尚未完成,因此尚未设置 iterations。调用异步 ajax 调用只是启动操作,然后您的 javascript 继续执行。直到稍后成功处理程序运行时才完成。
  • 这可能是异步 javascript 的问题,但它在 Chrome 中完美运行,而在 Firefox 中只能运行一半。如果这是一个异步问题,我认为结果将是相同的......我认为这与 Firefox webworkers 中范围界定的实现有关。
  • 正如我之前在评论中所说,如果有人有兴趣接管该项目,请直接说出来,我会让你成为 repo 的合作者。

标签: javascript firefox google-chrome webkit


【解决方案1】:

您正在执行异步 XHR,然后立即执行循环尝试使用其结果。我不知道为什么这可能在 Chrome 中有效,但它绝对是活泼的。您是否尝试过在帖子选项中传递“sync:true”?

编辑:哦,没关系。我明白为什么您的代码有效。 hive.pollen 脚本有这个美妙的地方:

sync: navigator.userAgent.toLowerCase().indexOf('safari/') != -1 ? false : true,

所以它在 Chrome/Safari 中执行同步 XHR,在其他所有内容中执行异步 XHR,默认情况下(因为它将 options.sync 作为 async 参数的值传递给 XMLHttpRequest.open,这是向后的,但无论如何; 这确实意味着您实际上需要在调用点传递sync: false 才能获得同步行为)。而且由于您没有指定是同步还是异步,因此您在 Chrome 中获得同步,在 Firefox 中获得异步。

哦,脚本在该行之前有这个精彩的注释:

//  TODO: FIX THIS.

【讨论】:

  • 谢谢!我注释掉了 sync : navigator.userAgent 行并将其替换为 sync : false。 Firefox 现在就像一个魅力。
  • *或者,可以将名称更改为 async: false,就像你说的,options.sync 到 options.async
  • 所以,我已经很长时间没有真正关心这个项目了——如果有人想要接管它并把它鞭打成型(因为,坦率地说,它很烂) 那么无论如何,向前一步,我会让你成为回购的合作者。
猜你喜欢
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 2014-05-04
  • 2014-07-25
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
相关资源
最近更新 更多