我在全局范围内有一个变量,需要定期检查是否有更改。这就是我将在简单的JS中执行的操作:
let currentValue, oldValue;
setInterval(()=>{
if(currentValue != oldValue){
doSomething();
}
}, 1000)
如何使用完成Observables
?
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged();
或者,您可以选择提供比较器功能:
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged((oldValue, newValue) => <return true if equal>);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句