jQuery自动刷新div [英] Jquery auto refresh div
问题描述
jQuery自动刷新正在占用大量浏览器内存.有没有办法阻止这种情况.我每2秒刷新一次2格,但我将其最多移动了9秒和15秒,这有助于延长窗口在我的网站上保持的时间越长,直到最终浏览器崩溃之前,它所花费的内存更多.
Jquery auto refresh is using up a LOT of browser memory. Is there a way to stop this. I had a 2 div refreshing every 3 seconds but I moved it up to 9 and 15 seconds, It helped a little bit the longer the window stays open on my site the more memory it takes until finally the browser crashes.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
<script>
var auto_refresh = setInterval(
function ()
{
$('#details2').load('links2.php').fadeIn("slow");
}, 15000); // refresh every 10000 milliseconds</script>
推荐答案
您可以尝试跳过load(),而是使用$ .ajax.我知道load();是一个ajax请求,但我似乎还记得它获取了整个脚本.尝试请求脚本,进行数据库计算,然后将数据作为json返回.我假设您正在使用数据库请求中的数据发送完整的html.尝试用json代替.
You could try to skip the load() and use $.ajax instead. I know load(); is an ajax request but I seem to recall it fetches the whole script. Try requesting a script, do your database calculations and return the data as json. I assume you're sending complete html with the data from the database request. Try this with json instead.
您将获得数据作为对象,例如这样.
You'll get the data as an object, like this for example.
{"variable":"foo"}
然后,您可以使用简单的每条语句获取数据.
Then you can fetch the data with a simple each statement.
$.ajax({
url: "links2.php",
type: "POST",
dataType: "json",
success: function(data){
// data here is returned as objects since it's json
$.each(data, function(key, value) {
$("#details2").empty().append(value.variable);
});
}
});
即使您每隔一秒钟左右调用一次,我认为这也不应该泄漏您的内存并最终使您的浏览器崩溃.试试看,让我知道如何进行.
I think this shouldn't leak your memory and eventually crash your browser, even though you call it every other second or so. Give it a try and let me know how it goes.
祝你好运!
这篇关于jQuery自动刷新div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!