Answer the question
In order to leave comments, you need to log in
How to fix memory leaks in Google Chrome extension?
Good day.
I'm new to JavaScript and front-end in general, but had to write an extension for Google Chrome. And it seems that he googled something, thought of something and wrote it himself ... And it works like an extension, but it eats incredibly memory resources. The extension consists of one background.js + jquery. I think that the problem is in the setInterval () function, which holds the links and the garbage collector cannot release them.
Here's what the extension does:
-Regularly polls the server (pulls the php file).
-If received data from the server displays a message to the user.
Here is the text of the background.js file
chrome.notifications.onButtonClicked.addListener(notificationBtnClick);
var options = {
type: "basic",
title: "Basic message",
message: "Primary message to display",
priority: 2,
buttons: [{ title: "Find uid"}],
};
function doNotify(serverData){
var objs = JSON.parse(serverData);
var lengthOfCalls = Object.keys(objs).length;
for (i = 0; i < lengthOfCalls; i++){
var uid = objs[i]["uid"];
options.message = "Поступление от " + objs[i]["name"];
chrome.notifications.create(uid, options);
}
}
function notificationBtnClick(notID, iBtn) {
console.log(iBtn);
if (iBtn == "0")
{
var win = window.open("https://www.google.ru/#q=" + notID, '_blank');
win.focus();
}
}
setInterval(function() {
$.ajax("http://192.168.0.1/get_data.php").done(function(data){
if (data != "false") {
doNotify(data);
}
});
}, 3000);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question