JavaScript, stop additional event listeners

前端 未结 5 1592
盖世英雄少女心
盖世英雄少女心 2021-01-12 01:41

Imagine I have this code:

var myFunc1 = function(event) {
    alert(1);
}
var myFunc2 = function(event) {
    alert(2);
}

element.addEventListener(\'click\'         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 02:02

    There's a further problem: the order that event listeners are executed is undefined. You'll need to handle event dispatch on your own to get around this, which leads us to some variant of llimllib's suggestion.

    function dispatchSleightEvent(evt) {
        var listeners = evt.currentTarget.sleightListeners[evt.type];
        // can't use for-in because enumeration order is implementation dependent
        for (var i=0; i

    This code is completely untested. To stop event dispatch while on a single target, an event listener returns false. If you want more data hiding, you can rewrite the above from a functional programming standpoint, though this might introduce memory leaks.

提交回复
热议问题