Typescript 我可以在回调本身中结束firebase回调吗?
我想知道是否有任何方法可以在回调本身内部停止回调。我已经尝试使用ref.off()方法停止它,如下所示,但它仍将继续运行。有什么建议吗Typescript 我可以在回调本身中结束firebase回调吗?,typescript,firebase,firebase-realtime-database,Typescript,Firebase,Firebase Realtime Database,我想知道是否有任何方法可以在回调本身内部停止回调。我已经尝试使用ref.off()方法停止它,如下所示,但它仍将继续运行。有什么建议吗 ref.orderByChild('t').startAt(time.getTime()).on('child_added', function (snapshot) { const d = snapshot.val(); const x = Math.round(d.x * 100).toString(); const y = Math.round
ref.orderByChild('t').startAt(time.getTime()).on('child_added', function (snapshot) {
const d = snapshot.val();
const x = Math.round(d.x * 100).toString();
const y = Math.round(d.y * 100).toString();
if (that.selectedEnd && d.t > that.selectedEnd.getTime()) {
snapshot.ref.off('child_added');
console.log('STOP');
} else {
....
}
});
根据API文件: 返回函数 提供的回调函数未经修改返回。这只是 为了方便起见,如果要将内联函数传递给on(),但 存储回调函数,以便以后传递到off() 因此,您可以将其返回值传递给
off()
,在调用on()
的同一查询对象上调用:
根据API文件: 返回函数 提供的回调函数未经修改返回。这只是 为了方便起见,如果要将内联函数传递给on(),但 存储回调函数,以便以后传递到off() 因此,您可以将其返回值传递给
off()
,在调用on()
的同一查询对象上调用:
感谢您的快速响应。但它在侦听器上给出了一个错误:类型为“(a:DataSnapshot,b?:string)=>any”的参数不能分配给类型为“EventType”的参数。类型“(a:DataSnapshot,b?:string)=>any”不可分配给类型“child_removed”。像这样尝试过:query.off('child_added',listener),但也不起作用。可能是TypeScript绑定有问题。但根据API文档,这是你应该做的。感谢快速响应。但它在侦听器上给出了一个错误:类型为“(a:DataSnapshot,b?:string)=>any”的参数不能分配给类型为“EventType”的参数。类型“(a:DataSnapshot,b?:string)=>any”不可分配给类型“child_removed”。像这样尝试过:query.off('child_added',listener),但也不起作用。可能是TypeScript绑定有问题。但根据API文档,这是你应该做的事情。
const query = ref.orderByChild('t').startAt(time.getTime());
const listener = query.on('child_added', function (snapshot) {
...
query.off('child_added', listener);
});