Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 交叉侦听多个事件并触发回调_Javascript_Arrays_Events_Data Structures - Fatal编程技术网

Javascript 交叉侦听多个事件并触发回调

Javascript 交叉侦听多个事件并触发回调,javascript,arrays,events,data-structures,Javascript,Arrays,Events,Data Structures,我有一个需要监听的事件名称列表,存储在数组中,如下所示: var events = ['A', 'B']; 现在,我不确定将首先触发哪个事件,它可能非常不一致(取决于它们等待的HTTP请求),因此我永远无法安全地只监听其中一个。所以,为了触发我最初的回调,我需要以某种方式“交叉监听”所有这些消息 所以我的想法是做以下几点: 为a创建一个侦听器,这将为B创建一个侦听器。B的侦听器将触发回调 为B创建一个侦听器,这将为a创建一个侦听器。a的侦听器触发回调 这将产生4个Listner,看起来像这样(

我有一个需要监听的事件名称列表,存储在数组中,如下所示:

var events = ['A', 'B'];
现在,我不确定将首先触发哪个事件,它可能非常不一致(取决于它们等待的HTTP请求),因此我永远无法安全地只监听其中一个。所以,为了触发我最初的回调,我需要以某种方式“交叉监听”所有这些消息

所以我的想法是做以下几点:

  • 为a创建一个侦听器,这将为B创建一个侦听器。B的侦听器将触发回调
  • 为B创建一个侦听器,这将为a创建一个侦听器。a的侦听器触发回调
  • 这将产生4个Listner,看起来像这样(伪代码):

    所以我相信这会解决我的问题(可能还有另一个问题,我在这里没有看到)

    问题是,当我只需要听两个事件时,我就可以做到这一点。但是当我有3-4个我想“交叉聆听”的事件时呢?假设我们有
    ['A','B','C','D']
    。这显然需要通过事件进行循环。这一部分让我困惑,我不知道如何继续。这需要注册一个很好的事件组合

    这需要在JavaScript中完成


    在这种情况下,我的想象力和逻辑是有限的。

    有很多方法可以满足您的要求,但为了保持简单,您可以像您已经做的那样,拥有一个事件名称数组,并在事件发生时将其删除,每次检查数组是否为空。像这样

    var events = ['A', 'B'];
    
    var callback = function() { console.log('The callback'); };
    
    var eventOccured = function(eventName) {
        // if the event name is in the array, remove it...
        var idx = events.indexOf(eventName);
        if (idx !== -1) {
            events.splice(events.indexOf(idx), 1);
        }
    
        // if the event array is empty then we've handled everything...
        calback();
    };
    
    Event.on('A', function () {
        // do whatever you need in the event handler
        eventOccured("A");
    });
    
    Event.on('B', function () {
        // do whatever you need in the event handler
        eventOccured("B");
    });
    

    我是这样想的:

    var callback=function(){console.log('The callback');};
    var事件={
    “单击”:false,
    “mouseover”:false,
    “mouseout”:错误
    };
    用于(事件中的道具){
    $('.evt按钮')。打开(道具,功能(evt){
    if(事件[evt.type]==false){
    console.log('First'+evt.type+'event');
    事件[evt.type]=真;
    checkAll();
    }
    });
    }
    函数checkAll(){
    var anyFalse=假;
    用于(事件中的道具){
    if(events.hasOwnProperty(prop)){
    如果(事件[prop]==false){
    anyFalse=true;
    打破
    }
    }
    }
    如果(!anyFalse){
    回调();
    }
    }
    
    
    按钮
    你不能用一个布尔值数组来代替,然后检查所有的都是
    真的
    ?您在各自的
    事件中将每个设置为
    true
    。我希望能举一个例子作为答案,因为我现在由于多种原因感到头昏眼花,我甚至无法想象您在说什么。如果它解决了我的问题,我会很乐意接受它。@Dugi:看起来你答应接受Arg0n的回答,而这个答案确实收到了。我稍微编辑了一下我的答案。如果有更多的事件,比如
    ['A','B','C','D']
    ,该怎么办。然后,我显然需要对它进行循环。这就是我的困惑所在。这仍然有效。将事件名称添加到数组中,并添加调用
    eventoccurrend(“EventName”)
    的事件处理程序。
    var events = ['A', 'B'];
    
    var callback = function() { console.log('The callback'); };
    
    var eventOccured = function(eventName) {
        // if the event name is in the array, remove it...
        var idx = events.indexOf(eventName);
        if (idx !== -1) {
            events.splice(events.indexOf(idx), 1);
        }
    
        // if the event array is empty then we've handled everything...
        calback();
    };
    
    Event.on('A', function () {
        // do whatever you need in the event handler
        eventOccured("A");
    });
    
    Event.on('B', function () {
        // do whatever you need in the event handler
        eventOccured("B");
    });