简体   繁体   English

Eventlisteners有影响吗?

[英]Eventlisteners impact?

What impact does eventlisteres have? 事件监听者有什么影响? Im talking about big numbers, here's an example: 我在谈论大数字,这是一个例子:

  • There's only x amount of .marker at first 最初只有x.marker
  • All children are added via JS when .marker is clicked - eventlistener 单击.marker时,通过JS添加所有子级.marker
  • Each child does it's own thing which means each of them have their own eventlisteners 每个孩子都做自己的事,这意味着他们每个人都有自己的事件监听器

<!-- Final HTML of single .marker when it has been clicked -->
<div class="marker">
    <div class="remove"></div>
    <div class="change"></div>
    <div class="add"></div>
    <div class="drag"></div>
</div>

var count = 20 000;

for(i = 0; i < count; i++) {

    var marker = document.createElement('div');
    marker.className = 'marker';
    someParentElement.appendChild(marker);

    marker.click( function() {
        //Create child elements
        var remove = document.createElement('div');
        remove.className = 'remove';
        marker.appendChild(remove);

        var change = document.createElement('div');
        change.className = 'change';
        marker.appendChild(change);

        var add = document.createElement('div');
        add.className = 'add';
        marker.appendChild(add);

        var drag = document.createElement('div');
        drag.className = 'drag';
        marker.appendChild(drag);

        //Children eventlisteners
        remove.click( function() {
            //Do it's thing
        });
        change.click( function() {
            //Do it's thing
        });
        add.click( function() {
            //Do it's thing
        });
        drag.click( function() {
            //Do it's thing
        });
    });
}

Please don't mind other things, eg creating 20 000 elements programmatically. 请不要介意其他事情, 例如以编程方式创建2万个元素。 My question is this: what would be the impact of having all these eventlisteners with all this code in them? 我的问题是: 将所有这些事件监听器与所有这些代码一起放入会产生什么影响? Does it even matter what or how much code is inside eventlistener as long as it hasn't been triggered? 只要未触发事件监听器,则事件监听器中包含什么代码或多少代码都无关紧要?

Try using event delegation , single event handler. 尝试使用事件委托(单个事件处理程序)。 See switch , .is() 参见switch.is()

 var count = 100; for (i = 0; i < count; i++) { var marker = document.createElement('div'); marker.className = 'marker'; marker.innerHTML = marker.className + " " + i; document.body.appendChild(marker); //Create child elements var remove = document.createElement('div'); remove.className = 'remove'; remove.innerHTML = "remove" + i; marker.appendChild(remove); var change = document.createElement('div'); change.className = 'change'; change.innerHTML = "change" + i; marker.appendChild(change); var add = document.createElement('div'); add.className = 'add'; add.innerHTML = "add" + i; marker.appendChild(add); var drag = document.createElement('div'); drag.className = 'drag'; drag.innerHTML = "drag" + i; marker.appendChild(drag); //Create child elements } var check = function(args) { alert(args.innerHTML.replace(/[^\\d+]/g, "")) } var obj = { remove: check, change: check, add: check, drag: check } var message = function(name) { console.log(name) } $("body").on("click", ".marker", function(event) { var name = event.target.className; switch (name) { case "remove": /* do stuff */ message(name); break; case "change": /* do stuff */ message(name); break; case "add": /* do stuff */ message(name); break; case "drag": /* do stuff */ message(name); break; default: /* do stuff */ alert(name); break; } // utilizing `.is()` if ($(event.target).is(".remove")) { // do stuff event.target.innerHTML += "clicked" } if ($(event.target).is(".change")) { // do stuff event.target.innerHTML += "clicked" } if ($(event.target).is(".add")) { // do stuff event.target.innerHTML += "clicked" } if ($(event.target).is(".drag")) { // do stuff event.target.innerHTML += "clicked" } if (!$(event.target).is(".marker")) { // utilizing an object obj[event.target.className](event.target) } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> 

Creating event handlers for so many things which are repeated feels like a waste of CPU cycles, plus memory to manage so many event listeners. 为许多重复的事件创建事件处理程序就像浪费CPU周期,再加上用于管理这么多事件侦听器的内存。

Instead, it would be preferrable to use event bubbling/delegation to listen to the click events from a parent node (as close an ancestor element as possible would be ideal) and see what element triggered the event and call the appropriate code accordingly. 相反,最好使用事件冒泡/委托来侦听来自父节点的单击事件(尽可能靠近祖先元素是理想的),并查看是哪个元素触发了该事件并相应地调用适当的代码。

This would be a one-time bind, and should also catch dynamically added elements if done right. 这将是一次绑定,并且如果正确完成的话,还应该捕获动态添加的元素。

Examples with jQuery that are also explained quite well include the following jQuery的示例也得到了很好的解释,包括以下内容

https://learn.jquery.com/events/event-delegation/ http://api.jquery.com/on/ https://learn.jquery.com/events/event-delegation/ http://api.jquery.com/on/

Though you are not limited to just jQuery to implement this. 虽然您不仅限于jQuery来实现此目的。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM