代码之家  ›  专栏  ›  技术社区  ›  Pablo

Firefox扩展正在冻结Firefox,直到请求完成

  •  1
  • Pablo  · 技术社区  · 15 年前

    出于某种原因,该函数将与firefox一起冻结,直到它从请求的站点完全检索流为止。有没有什么机制可以防止冰冻,所以它能像预期的那样工作?

    苏尔

    <statusbarpanel id="eee_label" tooltip="eee_tooltip" 
          onclick="eee.retrieve_rate(event);"/>
    

    JavaScript

    retrieve_rate: function(e)
     {
      var ajax = null;
      ajax = new XMLHttpRequest();
      ajax.open('GET', 'http://site.com', false);
      ajax.onload = function()
      {
       if (ajax.status == 200)
       {
        var regexp = /blabla/g;
        var match = regexp.exec(ajax.responseText);
        while (match != null)
        {
         window.dump('Currency: ' + match[1] + ', Rate: '
                              + match[2] + ', Change: ' +  match[3] + "\n");
         if(match[1] == "USD")
          rate_USD = sprintf("%s:%s", match[1], match[2]);
         if(match[1] == "EUR")
          rate_EUR = sprintf("%s:%s", match[1], match[2]);
         if(match[1] == "RUB")
          rate_RUB = sprintf("%s/%s", match[1], match[2]);
         match = regexp.exec(ajax.responseText);
        }
    
        var rate = document.getElementById('eee_label');
        rate.label = rate_USD + "  " + rate_EUR + "  " + rate_RUB;
       }
       else
       {
    
       }
      };
      ajax.send();
    

    我试着把 window.dump() 刚好在…之后 ajax.send() 请求完成后,它也会转储到控制台中。

    1 回复  |  直到 15 年前
        1
  •  4
  •   SLaks    15 年前

    您需要通过传递 true 作为最后一个参数 ajax.open .

    注意,一旦你这样做了, send 函数将立即返回,之后的任何代码都将在请求完成之前运行。