jquery - anonymous function in javascript -


i pass anonymous function-a parameter function-b. after call b.destroy, function exists , gets executed, live anonymous scope itself. found unexpected behavior through debugger. following code.

var  builder.record.verifyexplorer = new builder.verifyexplorer(     window.bridge.getrecordingwindow(),     builder.getscript().seleniumversion,     function(step) {       builder.getscript().addstep(step);       builder.stepdisplay.update();       // don't stop: cause listener prevents click       // activating selected element detached prematurely.       settimeout(function() { builder.record.stopverifyexploring(); }, 1);       window.bridge.focusrecorderwindow();     }   ); 

i destroy above function defining stopverifyexploring be

builder.record.stopverifyexploring = function() {   builder.record.verifyexploring = false;   builder.record.verifyexplorer.destroy();   builder.record.verifyexplorer = null;   builder.record.continuerecording(); }; 

even after verifyexplorer.destroy called function(step) live in anonymous scope , gets executed , unwanted things.

i have weird behavior replacing

  jquery(frame.document).bind(l, {}, ae.listeners[l], true); 

with

  frame.document.addeventlistener(l,ae.listeners[l],true); 

in verifyexplorer. how me changing above piece code lead unexpected behavior?

all part of me fixing bug in open source project sebuilder. related post

the behavior isn't unexpected @ all, you've done nothing cancel timer.

if want cancel timer, save return value of settimeout , pass value cleartimeout later when/if want cancel timer.

what's going on:

functions , objects exist long has reference them. when do:

settimeout(function() { builder.record.stopverifyexploring(); }, 1); 

...you're saving reference anonymous function in browser's timer handling stuff. function has reference context in created (and containing contexts). if verifyexplorer = null, has no effect on verifyexplorer object used refer at all, nor referring object. clears reference object that specific variable. if there other outstanding references (and there are), object kept in memory.

in case, if outstanding reference function you've given settimeout, clearing timeout (probably in destroy) release browser's reference function, releases function's references contexts closes over, , things eligible garbage collection.

more (on blog): closures not complicated


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -