Javascript Ajax—更新内容时浏览器会延迟
我正在尝试实时更新状态页 我正在使用Ajax更新页面。更新设置为每3秒更新一次。但无论何时调用更新,浏览器都会冻结至少一两秒钟Javascript Ajax—更新内容时浏览器会延迟,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我正在尝试实时更新状态页 我正在使用Ajax更新页面。更新设置为每3秒更新一次。但无论何时调用更新,浏览器都会冻结至少一两秒钟 <script type="text/javascript"> window.onload = updateStatus; function updateStatus() { updateinfo(); setTimeout(updateStatus, 3000); } function g
<script type="text/javascript">
window.onload = updateStatus;
function updateStatus() {
updateinfo();
setTimeout(updateStatus, 3000);
}
function getJson(theUrl, update) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
update(xmlhttp.responseText);
}
}
xmlhttp.open("GET", theUrl, false);
xmlhttp.send();
}
function updateinfo() {
getJson('backend/status', function(update) {
var jsono = JSON.parse(update);
document.getElementById('name').innerHTML = jsono.name;
document.getElementById('online').innerHTML += jsono.online;
document.getElementById('ip').innerHTML = jsono.ip + ':';
document.getElementById('ip').innerHTML += jsono.port;
document.getElementById('memory').innerHTML = jsono.memory + " MB";
});
}
</script>
window.onload=updateStatus;
函数updateStatus(){
updateinfo();
setTimeout(updateStatus,3000);
}
函数getJson(URL,更新){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
更新(xmlhttp.responseText);
}
}
open(“GET”,theUrl,false);
xmlhttp.send();
}
函数updateinfo(){
getJson('backend/status',函数(更新){
var jsono=JSON.parse(更新);
document.getElementById('name').innerHTML=jsono.name;
document.getElementById('online').innerHTML+=jsono.online;
document.getElementById('ip').innerHTML=jsono.ip+':';
document.getElementById('ip').innerHTML+=jsono.port;
document.getElementById('memory').innerHTML=jsono.memory+“MB”;
});
}
如果有人能给我一些改进的建议。让它不那么迟钝或者让它消失
2) 我一直在考虑使用JQuery。我应该采取行动吗?利与弊?另外,与JavaScript相比,JQuery的性能如何?您让AJAX请求同步运行——这是您永远不需要的,因为这首先会阻止它成为AJAX,因为A代表异步
将
xmlhttp.open
调用的第三个参数更改为true
(或者忽略它,因为这是默认值)。@RoTaRa谢谢!它解决了这个问题!