Reputation: 677
I have the following javascript code, to loop through a number of pages on my server (Used for digital signage).
This code is used in all pages and (at the moment) loops at a page every 3 seconds (see timeout). But, the memory usage of the browser goes up, slowly but steady. After 2 hours it went from 192mb in use to 436mb in use. Since this is on a Raspberry pi with only 512mb memory dedicated to cpu it's not very practical.
Are there any obvious memory leaks in this code? I'm not an expert myself, but since these things will be running 8-12hours a day probably I'm talking about 20reloads/min, so +/- 9600-14400 reloads a day. More if it doesn't get shutdown..
$(document).ready(function() {
versionPage = parseInt(document.getElementById("version").innerHTML);
versionServer = 0
urls = 0;
getVersion();
currentPage = getPage();
getContent();
main();
function getPage() {
page = window.location.href.split("/");
return page[page.length-1];
}
function getVersion() {
$.ajax({
url: "http://localhost/getVersion",
type: "GET",
dataType: "json",
success: function(json) {
console.log("json" + json);
versionServer = json;
if (versionServer != versionPage) {
console.log("Difference!");
}
else {
console.log("Same!");
}
},
});
}
//saves how much urls there are
function getContent() {
$.ajax({
url: "http://localhost/getContent",
type: "GET",
dataType: "json",
success: function(json) {
console.log(json);
urls = json;
},
});
}
//main function loop
function main() {
//check version every
window.setInterval(function() {
getVersion();
if(versionServer != versionPage) {
window.location.href = "http://localhost:5000/1"
}
if(urls != 1) {
nextPage =(parseInt(currentPage) % urls) + 1;
window.location.href = "http://localhost:5000/" + nextPage;
}
}, 3000);
}
});
Upvotes: 0
Views: 159
Reputation: 31
I had to ask you this in comment but it required "50 reputation" to comment.
Have you tried putting your code in an external Javascript file, something like "signagelooper.js" and looing your pages sequentially. This way your looper functions always have one instance running. Correct me if this is what you do not want to do.
Upvotes: 1