Reputation: 3238
I am working on a dashboard project where multiple widgets (like a dozen) need to be updated via an AJAX source every 5 seconds or so. I am currently using setTimeout
to queue a widget update, which is called everytime the widget is actually updated (not technically a recursion, but…). This is how the code looks
var update;
(update = function() {
$.get(source, function() {
// Do something
setTimeout(update, 5000);
});
})();
After a few hours of the page running (Chromium 21), it can easily OOM my 8GB RAM. As the client uses IE6 on very small configurations (< 1GB RAM), the issue is even more important.
How could I avoid this quirk?
Upvotes: 0
Views: 374
Reputation: 3238
Ok, thanks freakish & Pointy, the leak was indeed caused by jqPlot. Thing is, you have to use the .destroy()
method on said plot, and then remove (and not empty) its DOM container, before creating another one and redrawing a plot.
Upvotes: 1
Reputation: 2942
Use setInterval instead. That way you only generate one timer and don't need to start a new one every five seconds, which might consume some sort of handle in the browser.
I actually wouldn't have thought setTimeout to leak, but there is a clearTimeout function. Just for researching purposes you could try saving the returnvalue of setTimeout and clearing it, before starting the next one.
Upvotes: 0