2013-10-07 67 views
0

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

widget.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 - 生态REBOX是托管在Heroku上一个Rails应用程序。我不确定哪些代码我不得不从那里告诉你,因为这是有道理的...让我知道,我会把它拉起来。

+0

响应时间通常与客户端无关。您是否尝试过使用Chrome开发人员工具联网时间线来确定导致延迟的原因? – adamb

+0

Chrome开发工具告诉我这需要21秒。如果你直接进入'json',那么它就是瞬间的。 – Dudo

+0

加载此页面需要1.0x秒。不知道问题出在哪里。 – kobaltz

回答

0

我有类似的情况,我的API调用在Windows 7中持续21秒,我发现问题在代理设置。我去了互联网选项/连接/局域网设置,并取消选中“自动检测设置”。我的API调用降至700毫秒。我猜这是一些超时发生,而它'检测设置'...