javascript - Infinte Loop - Adding and Removing Event Listeners -
i have syntaxed checked several times, cannot figure out why functions being recursively called:
function updatenewclasses() { function addnewclass() { updatenewclasses(); } window.addeventlistener( "hashchange", addnewclass ); // conditionals being skipped; no loops window.removeeventlistener( "hashchange", addnewclass ); location.href = '#'; window.addeventlistener( "hashchange", addnewclass ); }
the program keeps calling hash change function recursively calls updatenewclass
leading endless loop.
since addeventlistener
in updatenewclasses
called addnewclass
triggered addeventlistener
keep adding event listener each time triggered.
additionally, updatenewclasses
creating "new" addnewclass
not removed on subsequent call updatenewclasses
. want store function want add/remove outside scope of callback can removed.
perhaps mean this:
var addnewclass = function() { updatenewclasses(); } var updatenewclasses = function() { // remove old listener, change hash, , add listener again window.removeeventlistener("hashchange", addnewclass); location.href = '#'; window.addeventlistener("hashchange", addnewclass); }
Comments
Post a Comment