因此,每个请求21秒,snippet.json和图像。我的小部件在42秒内加载。一致。这看起来很奇怪。
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 - Eco ReBox是一款托管在Heroku上的rails应用。我不确定我要从那里向你展示哪些代码才有意义......让我知道,我会把它拉出来。
答案 0 :(得分:0)
我有类似的情况,我的api调用在Windows 7中持续21秒,我发现问题出在代理设置中。我转到了Internet选项/连接/局域网设置并取消选中了“自动检测设置”。我的api电话下降到700毫秒。我猜测它是在检测到设置时发生的某种超时......