如何断开与事件的连接
How to disconnect from events
我创建了一个 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);
});
});
});
}
- 第一次发生该事件时如何取消订阅?
- 如果其中一个事件是第一次发生,我如何取消订阅这两个事件?
这就够了吗?
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
您需要使用连接 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;
});
});
});
}
我创建了一个 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);
});
});
});
}
- 第一次发生该事件时如何取消订阅?
- 如果其中一个事件是第一次发生,我如何取消订阅这两个事件?
这就够了吗?
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
您需要使用连接 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;
});
});
});
}