javascript stop all functions when game is over -


i need stop function executing:

document.addeventlistener("keypress", function(evt) {      evt = evt || window.event;     var charcode = evt.keycode || evt.which;     var charstr = string.fromcharcode(charcode);      checkkey(charstr);      //check loss     if (attempt == 0) {         gamelost();     }      //check win     underscoreremain = namereform.includes("_");     if (!underscoreremain) {         gamewon();     }      document.getelementbyid("attempt").innerhtml = attempt; }); 

basically, when game either won or lost, won't take more key pressed, hence no execution of rest of functions. i've looked on w3school , stackoverflow, removeenventlistener , other suggested methods don't work.

remove event listener passing original function object removeeventlistener @ appropriate time.

notice gave function name.

document.addeventlistener("keypress", function handler(evt) {      // evt = evt || window.event; // line unnecessary      var charcode = evt.keycode || evt.which;     var charstr = string.fromcharcode(charcode);      checkkey(charstr);      //check loss     if (attempt == 0) {         gamelost();          // remove listener         this.removeeventlistener(event.type, handler);     }      //check win     underscoreremain = namereform.includes("_");     if (!underscoreremain) {         gamewon();          // remove listener         this.removeeventlistener(event.type, handler);     }      document.getelementbyid("attempt").innerhtml = attempt; }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -