0

因此,每个请求、snippet.json 和图像需要 21 秒。我的小部件在 42 秒内加载。始终如一。这似乎很奇怪。

小部件.js

<script type="text/javascript">
  function fetchJSONFile(path, callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
          var data = JSON.parse(httpRequest.responseText);
          if (callback) callback(data);
        };
      };
    };
    httpRequest.open('GET', path);
    httpRequest.send(); 
  }

  // this requires the file and executes a callback with the parsed result once available
  fetchJSONFile('http://ecorebox.com/companies/1/snippet.json', function(data){
    // do what you do
    var trees = data['trees'];
    var water = data['water'];
    var energy = data['electricity'];
    var widget = document.createElement("div");
    widget.id = 'erb_widget';
    widget.style.width = "200px";
    widget.style.height = "400px";
    widget.style.font = '14px';
    widget.style.src = "local('Oswald Light'), local('Oswald-Light'), url(http://themes.googleusercontent.com/static/fonts/oswald/v7/HqHm7BVC_nzzTui2lzQTDT8E0i7KZn-EPnyo3HZu7kw.woff) format('woff')";
    widget.style.color = '#666';
    widget.style.border = 'solid 1px #666';
    var logo = document.createElement("div");
    logo.id = 'erbw_logo';
    logo.style.background = "url(http://ecorebox.com/assets/narrow_200px_logo.png) no-repeat";
    logo.style.height = '29px';
    logo.style.width = '109px';
    logo.style.margin = '20px auto';
    var txt = document.createElement("div");
    txt.id = 'erbw_txt';
    txt.style.font = 'bold 13px';
    txt.style.margin = '5px';
    var cs_trees = document.createElement("div");
    cs_trees.id = 'erbw_cs_trees';
    cs_trees.style.background = "url(http://ecorebox.com/assets/erbw_ico_tree.png) no-repeat";
    cs_trees.style.height = '61px';
    cs_trees.style.margin = '20px';
    cs_trees.innerHTML = "<div style='margin: 12px auto auto 62px; font: 14px bold;'><span style='font: 22px bolder'>" + trees + "</span><br/>Trees Saved</div>";
    var cs_water = document.createElement("div");
    cs_water.id = 'erbw_cs_water';
    cs_water.style.background = "url(http://ecorebox.com/assets/erbw_ico_water.png) no-repeat";
    cs_water.style.height = '61px';
    cs_water.style.margin = '20px';
    cs_water.innerHTML = "<div style='margin: 12px auto auto 62px; font: 14px bold;'><span style='font: 22px bolder'>" + water + "</span><br/>Gallons of Water Conserved</div>";
    var cs_energy = document.createElement("div");
    cs_energy.id = 'erbw_cs_energy';
    cs_energy.style.background = "url(http://ecorebox.com/assets/erbw_ico_energy.png) no-repeat";
    cs_energy.style.height = '61px';
    cs_energy.style.margin = '20px';
    cs_energy.innerHTML = "<div style='margin: 12px auto auto 62px; font: 14px bold;'><span style='font: 22px bolder'>" + energy + "</span><br/>kWh Energy Saved</div>";
    var footer = document.createElement("div");
    footer.id = 'erbw_footer';
    footer.style.font = 'bolder 14px';
    footer.style.textAlign = 'center';
    footer.style.padding = '10px';
    footer.style.margin = '10px';
    footer.innerHTML = "Learn more at<br/><a href='http://www.ecorebox.com' target='_blank'>www.ecorebox.com</a><br/>(877) REBOX IT";

    // this appends the div to the site.  will be different for each client
    $('.content').append(widget);
    document.getElementById('erb_widget').appendChild(logo);
    document.getElementById('erb_widget').appendChild(txt);
    document.getElementById('erb_widget').appendChild(cs_trees);
    document.getElementById('erb_widget').appendChild(cs_water);
    document.getElementById('erb_widget').appendChild(cs_energy);
    document.getElementById('erb_widget').appendChild(footer);

  });
</script>

要查看它的实际效果,请查看http://alphabeticdesign.com/test - Eco ReBox 是托管在 Heroku 上的 Rails 应用程序。我不确定我必须从那里向您展示哪些代码才能有意义......让我知道,我会拉出来。

4

1 回答 1

0

我有类似的情况,我的 api 调用在 Windows 7 中持续花费 21 秒,我发现问题出在代理设置中。我去了 Internet 选项/连接/LAN 设置并取消选中“自动检测设置”。我的 api 调用下降到 700 毫秒。我猜这是“检测设置”时发生的某种超时......

于 2015-08-17T13:08:29.360 回答