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

javascript - Failed to execute 'addEventListener' on 'EventTarget' - Stack Overflow

programmeradmin6浏览0评论

I struggle with a javascript error, that I cannot get rid of: Uncaught TypeError: Failed to execute 'addEventListener' on 'EventTarget': The callback provided as parameter 2 is not an object.

This is the script, that is used in a cookie consent module after the tag:

window.addEventListener("load", setTimeout(   function(){
  window.cookieconsent.initialise({ 
  "palette": { "popup": {"background": "#DCDCDC"}, 
              "button": {"background": "#9bba44"}
              }, 
  "position": "bottom-right", 
  "content": { "message": "We use cookies.", 
               "accept": "Accept all", 
               "deny": "Decline all",
               "link": "Find out more." } 
  })
}, 3000));
</script>

If needed I can look into cookieconsent.initialise, but the error may be something else, more trivial to anyone with experience. What is the second parameter here?

I struggle with a javascript error, that I cannot get rid of: Uncaught TypeError: Failed to execute 'addEventListener' on 'EventTarget': The callback provided as parameter 2 is not an object.

This is the script, that is used in a cookie consent module after the tag:

window.addEventListener("load", setTimeout(   function(){
  window.cookieconsent.initialise({ 
  "palette": { "popup": {"background": "#DCDCDC"}, 
              "button": {"background": "#9bba44"}
              }, 
  "position": "bottom-right", 
  "content": { "message": "We use cookies.", 
               "accept": "Accept all", 
               "deny": "Decline all",
               "link": "Find out more." } 
  })
}, 3000));
</script>

If needed I can look into cookieconsent.initialise, but the error may be something else, more trivial to anyone with experience. What is the second parameter here?

Share Improve this question asked Mar 12, 2020 at 13:35 Peter Nemeth .malomsok.Peter Nemeth .malomsok. 5101 gold badge4 silver badges15 bronze badges 3
  • 2 This is a basic issue. Your setTimeout is not a function but a function call. Wrap it in yet another function. – Wiktor Zychla Commented Mar 12, 2020 at 13:38
  • Yes! That was it! Many thanks! – Peter Nemeth .malomsok. Commented Mar 12, 2020 at 13:50
  • @wiktor-zychla This was quick. Thanks – K Shaikh Commented Mar 22, 2021 at 16:30
Add a comment  | 

1 Answer 1

Reset to default 22

The 2nd parameter of window.addEventListener should be a function.

What you have boils down to:

window.addEventListener("load", setTimeout(function(){ /* stuff */}, 3000));

That setTimeout is called the moment you call addEventListener, and the return value of setTimeout (The timeout id) is passed to addEventListener.

You need to wrap the setTimeout in a function:

window.addEventListener("load", () => setTimeout(function(){
    /* stuff */
}, 3000));

Now, you're passing a function to addEventListener that can be called on the load event. That function will set a new timeout.

发布评论

评论列表(0)

  1. 暂无评论