最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

addeventlistener - Uncaught RangeError: Maximum call stack size exceeded, JavaScript - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question asked Feb 29, 2012 at 10:57 anony_rootanony_root 1,5274 gold badges16 silver badges25 bronze badges 1
  • developer.mozilla/en/DOM/element.removeEventListener the third parameter flags whether or not you want the event listener to use event capturing (as oppose to bubbling) on adding, and on removal whether or not the event was added as such. – davin Commented Feb 29, 2012 at 11:04
Add a ment  | 

2 Answers 2

Reset to default 9

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);

Or you might have two Javascript functions with the same name.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论