Am I trying to add an event listener to all objects except a few selected ones (the selected ones also have arbitrary children at arbitrary levels)?
I asked this question before, but in fact I did not get an answer to it. I came up to solve this problem. Could you help me debug it?
I add an event listener to the body element called bodylistener
, and an event listener for several selected elements called selectedElementsMarkTrue
. A few selected elements that I do not want to execute any code, the selectedElementsMarkTrue
listener pior to the bodylistener
using the setTimeout function
. selectedElementsMarkTrue
set the selectedElementsMarkTrue
variable to true
and the bodylistener
checks to see if selectedElements
true
before executing the som code. There is something wrong with my code.
var selectedElements = false; var bodylistener = function(){ window.setTimeout(function(){//Setting timeout so that the other handler, selectedElementsMarkTrue, always performs first (function(){ if(selectedElements == false){ //Do some stuff }else{ selectedElements = false; } }); }, 10);//Could be 0, but te be sure I set 10 }; var selectedElementsMarkTrue = function(){ selectedElements = true; }; $('#dontAddEventListener1, #dontAddEventListener2').each(function(){ this.addEventListener('click', selectedElementsMarkTrue, false); }); document.body.addEventListener('click', bodylistener, false);
Can't I get the setTimeout function to execute base code?
source share