Uncaught RangeError: Maximum call stack size exceeded, JavaScript

anony_root picture anony_root · Feb 29, 2012 · Viewed 36.2k times · Source

I have a problem

    open: function($type) {
          //Some code
          document.getElementById($type).addEventListener("click", l.close($type), false);
    },
    close: function($type) {
           //There is some code too
           document.getElementById($type).removeEventListener("click", l.close($type), false);
           //^ Recursion & Uncaught RangeError: Maximum call stack size exceeded
    }

What I'm doing wrong? Without this click event listener everything is working well. And what is the third parameter doing (true|false)? Thank you.

Answer

Lycha picture Lycha · Feb 29, 2012

You are calling the close function in the addEventListener and removeEventListener when you are trying to pass is as an argument (causing an infinite loop). Instead you should simply pass the reference to the function as follows:

document.getElementById($type).addEventListener("click", l.close, false);

And:

document.getElementById($type).removeEventListener("click", l.close, false);