Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  166] [ 6]  / answers: 1 / hits: 28009  / 12 Years ago, sat, may 5, 2012, 12:00:00

I am trying to remove the onRequest listener added by chrome.extension.onRequest.addListener after a request is made, like this:



chrome.extension.onRequest.addListener(
function(request){
chrome.extension.onRequest.removeListener();
other_function(request);
}
);


The problem is that I don't know if this works or not. I tried chrome.extension.onRequest.hasListener, which seems not to give the right answer, so I am wondering if there are some other ways to remove the onRequest listener or check if the listener exists or not.



Thanks!


More From » events

 Answers
12

removeListener takes an argument. You need to name the listener function and then remove it by name:



function doStuff(request){
chrome.extension.onRequest.removeListener(doStuff);
other_function(request);
}
chrome.extension.onRequest.addListener(doStuff);


Or, more succinctly:



chrome.extension.onRequest.addListener(
function doStuff(request){
chrome.extension.onRequest.removeListener(doStuff);
other_function(request);
}
);

[#85768] Friday, May 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monicag

Total Points: 651
Total Questions: 106
Total Answers: 104

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
;