Ajax setInterval导致浏览器无响应 [英] Ajax setInterval causing browser to be unresponsive
本文介绍了Ajax setInterval导致浏览器无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下测试代码:
I have the following test code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ajax - one variable test</title>
<style>
body{ font-size: 12px; font-family: Arial;}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<ol id="variable1"><var1></ol>
<script>
setInterval(function()
{
$("#variable1").load("ajax_v00.html")
},3000);
</script>
</body>
</html>
我正在使用向var1报告状态的嵌入式Web服务器。
I am using an embedded web-server that reports a status to var1.
示例:Ok or Head Up
example: Ok or Head Up
上述操作会崩溃我测试的任何浏览器。有人可以看到我是否做了一些
错误的东西吗?
The above is crashing any browser that I tested on. Can someone see if I am doing something wrong?
谢谢。
推荐答案
请求可能需要超过3秒才能执行堆叠请求,直到浏览器无法处理所有请求并导致崩溃...而不是使用时间间隔,您应该使用超时来执行请求3最后一次完成后的秒数。这里:
The request might take more than 3 seconds to execute thus stacking the requests until the browser cannot handle it all and crash... Instead of using an interval, you should use a timeout that will execute the request 3 seconds AFTER the last one finished. Here:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ajax - one variable test</title>
<style>
body{ font-size: 12px; font-family: Arial;}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<ol id="variable1"><var1></ol>
<script>
function doRefresh()
{
$.ajax({
url:"ajax_v00.html",
success:function(data){
$("#variable1").html(data);
setTimeout(doRefresh,3000);
}
});
}
setTimeout(doRefresh,3000);
</script>
</body>
</html>
这篇关于Ajax setInterval导致浏览器无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文