I have an event, which can fire itself. I try to make the code as efficient as possible, but it can hit maximum call stack in some circumstances, which are out of my control. It's not an infinite stack and it will end at some point, but sometimes it can potentially crash before it finishes because of the limit.
Will I increase the number of call stack if I set up 2 similar event listeners and split the code? Or what can I do?
UPDATE: It's on DOM change event (working with Webkit only, so don't care about other browsers), which can also modify the DOM based on some conditions. I haven't really hit that limit yet, but theoritically, it potentially can. I'm still optimizing the code to make as less DOM manipulations as possible.
UPDATE 2: I'm including sample (not real) example:
document.addEventListener('DOMSubtreeModified', function(event){
this.applyPolicy(event);
}, true);
function applyPolicy(event){
if( typeof event != "undefined" ){
event.stopPropagation();
event.stopImmediatePropagation();
}
if( !isButtonAllowed ){
$('button:not(:disabled)').each(function(){
$(this).attr('disabled', true);
});
}
}
This is just a sample code, but even in this case, if you have say 100s of buttons, the call stack will be in 100s too. Note that if you use $('button').attr('disabled', true);
, this will cause call stack problem, because jQuery will be trying to modify the DOM infinitely.