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

javascript - What is initEvents() and should I use it in 2018? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to understand the code of another programmer and there is a initEvents () method. Some articles tells that this is an outdated method, but I'm not sure. Could you help me? What it is? And is it really outdated?

I'm trying to understand the code of another programmer and there is a initEvents () method. Some articles tells that this is an outdated method, but I'm not sure. Could you help me? What it is? And is it really outdated?

Share Improve this question asked Jan 9, 2018 at 14:46 Bogdana PavlovaBogdana Pavlova 711 gold badge1 silver badge2 bronze badges 1
  • 3 read this you shouldn't use this... – user757095 Commented Jan 9, 2018 at 14:48
Add a ment  | 

2 Answers 2

Reset to default 11

This is indeed a deprecated method which will init the event before dispatching, you should instead use the Event constructor which have a second argument for event initialisation and then dispatch the event as you like.
Something like this:

// create a look event that bubbles up and cannot be canceled

var evt = new Event("look", {"bubbles":true, "cancelable":false});
document.dispatchEvent(evt);

// event can be dispatched from any element, not only the document
myDiv.dispatchEvent(evt);

initEvents has been deprecated.

Instead use this to Create Events :

const customEvent = new Event("CustomEventEmmiter", {"bubbles":true, "cancelable":false});
document.dispatchEvent(customEvent);

And listen to it like this :

window.addEventListener("CustomEventEmmiter", function(){
  alert('CustomEventEmmiter TRIGGERED');
});

Check out more about Events here

发布评论

评论列表(0)

  1. 暂无评论