0

我创建了一个 js 脚本,它订阅了两个不同的事件:

function testDoubleEvents() {
    alert("started");
    var eventName1 = "LeftBumperPressed";
    var eventName2 = "RightBumperPressed";

    session.service("ALMemory").done(function (ALMemory) {
        alert("ALMemory ok");
        ALMemory.subscriber(eventName1).done(function(subscriber1) {
            alert("subscribed1");
            subscriber1.signal.connect(function(result1){
                alert(eventName1);
            });
        });

        ALMemory.subscriber(eventName2).done(function(subscriber2) {
            alert("subscribed2");
            subscriber2.signal.connect(function(result2){
                alert(eventName2);
            });
        });

    });
}
  1. 第一次发生此事件时,如何取消订阅该事件?
  2. 当其中一个事件第一次发生时,我如何取消订阅这两个事件?
4

2 回答 2

2

这样就够了吗?

alert("subscribed1");
        subscriber1.signal.connect(function(result1){
            alert(eventName1);
            subscriber1.signal.disconnect()
        });

http://doc.aldebaran.com/2-5/dev/js/index.html?highlight=javascript#signals

于 2017-11-27T08:43:12.867 回答
2

您需要使用连接 ID 调用断开连接,但您需要保留对所有这些订阅者和连接的引用。在这里,eventName1 将在第一次发生时断开连接,而 eventName2 将两者都断开连接。

function testDoubleEvents() {
    alert("started");
    var eventName1 = "LeftBumperPressed";
    var eventName2 = "RightBumperPressed";
    var subscriber1 = 0;
    var subscriber2 = 0;
    var connectionId1 = 0;
    var connectionId2 = 0;

    session.service("ALMemory").done(function (ALMemory) {
        alert("ALMemory ok");
        ALMemory.subscriber(eventName1).done(function(subscriber) {
            subscriber1 = subscriber;
            alert("subscribed1");
            subscriber1.signal.connect(function(result1){
                alert(eventName1);
                subscriber1.signal.disconnect(connectionId1);
            }).then(function(connection) { 
                connectionId1 = connection; 
            });
        });

        ALMemory.subscriber(eventName2).done(function(subscriber) {
            alert("subscribed2");
            subscriber2 = subscriber;
            subscriber2.signal.connect(function(result2){
                alert(eventName2);
                subscriber2.signal.disconnect(connectionId2);
                subscriber1.signal.disconnect(connectionId1);
            }).then(function(connection) { 
                connectionId2 = connection; 
            });
        });

    });
}

作为记录,文档在这里:http ://doc.aldebaran.com/2-5/dev/js/index.html

于 2017-11-29T00:24:34.940 回答