removeEventListener on anonymous functions in JavaScript -


i have object has methods in it. these methods put object inside anonymous function. looks this:

var t = {}; window.document.addeventlistener("keydown", function(e) {     t.scroll = function(x, y) {         window.scrollby(x, y);     };     t.scrollto = function(x, y) {         window.scrollto(x, y);     }; });   

(there lot more code, enough show problem)

now want stop event listener in cases. therefore trying removeeventlistener can't figure out how this. have read in other questions not possible call removeeventlistener on anonymous functions, case in situation?

i have method in t created inside anonymous function , therefore thought possible. looks this:

t.disable = function() {     window.document.removeeventlistener("keydown", this, false); } 

why can't this?

is there other (good) way this?

bonus info; has work in safari, hence missing ie support.

i believe point of anonymous function, lacks name or way reference it.

if create named function, or put in variable have reference it.

var t = {}; var handler = function(e) {     t.scroll = function(x, y) {         window.scrollby(x, y);     };     t.scrollto = function(x, y) {         window.scrollto(x, y);     }; }; window.document.addeventlistener("keydown", handler); 

you can remove by

window.document.removeeventlistener("keydown", handler);    

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -