2011-12-16 87 views
0

我有一个在现代基于webkit的浏览器(http://montecarlo-tester.appspot.com/)中工作得很好的web应用程序。基本上它使用webworker从服务器获取数据,然后在执行一些计算后将其发回。web工作人员在webkit中的行为与Firefox不同的地方

它在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 
} 
+2

“不幸的是,火狐缺乏一个调试器”您是否尝试过[萤火虫](http://getfirebug.com)? – 2011-12-16 01:03:37

+2

在`var iterations`和`var key`的注释之前在变量名后面加分号会有什么区别吗? – jfriend00 2011-12-16 01:04:02

回答

4

你做一个异步XHR,然后马上做一个循环试图利用其结果。我不知道为什么这可能适用于Chrome,但它绝对是活泼的。你有没有尝试在你的发布选项中传递“sync:true”?

编辑:哦,没关系。我明白你的代码为什么起作用。该hive.pollen脚本有这个美好位:

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

所以它做在Chrome/Safari浏览器同步XHR和一切异步之一,在默认情况下(因为它传递options.sync作为async参数值XMLHttpRequest.open,这是倒退,但无论如何;这确实意味着您实际上需要在调用站点传递sync: false以获得同步行为)。由于您不指定是要同步还是异步,因此您可以在Chrome中获得同步,在Firefox中获得异步。

哦,脚本有该行此前精彩点评:

// TODO: FIX THIS. 
相关问题