I have a web page that constantly calls a php page using $.getJSON to get some info. I am looping using javascript setTimeOut. It works fine except the memory usage grows when running it.
Ive read in places that you have to be careful when creating dynamic DOM elements and to be sure to fully remove them to avoid mem leaks in IE and Firefox but I am not even doing that in this test page. (I will be later)
My end goal is to have a web application that updates the users page on a database change from other serer side applications or other events. So my idea is to have a javascript constantly call a php script that returns the file time on the database, if db appears to have change get the database info.
Heres my code:
<!doctype html>
<html lang="en">
<head>
<title>Test For Mem Leaks</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
开发者_Python百科 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
function init(){
setTimeout("call()",250);
}
function call(){
var p = "test";
var f = "returnDBTime";
var a = name;
$.getJSON ('gateway/gateway.php', { p:p, f:f ,a:a }, function (rere) {
setTimeout("call()",250);
});
}
</script>
</head>
<body>
</body>
</html>
<script type="text/javascript">
init();
</script>
Instead of setTimeout
and than setting it again in the callback u can use setInterval
And you are not creating any DOM nodes here so you shouldn't have any memory leaks. Please detail your ajax callback function because this code seems to work fine.
I'm not sure if the setTimeout (even if unsynchronized), will erase the context so your vars can be discarded. Have you tried setting values to null before calling timeout?
精彩评论