如何检测变量的变化?
How to detect change in a variable?
我有一个全局范围内的变量,我需要定期检查它是否有变化。这就是我在简单 JS 中的做法:
let currentValue, oldValue;
setInterval(()=>{
if(currentValue != oldValue){
doSomething();
}
}, 1000)
如何使用 Observables
完成?
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged();
或者您可以选择给出 comparator-function:
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged((oldValue, newValue) => <return true if equal>);
我有一个全局范围内的变量,我需要定期检查它是否有变化。这就是我在简单 JS 中的做法:
let currentValue, oldValue;
setInterval(()=>{
if(currentValue != oldValue){
doSomething();
}
}, 1000)
如何使用 Observables
完成?
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged();
或者您可以选择给出 comparator-function:
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged((oldValue, newValue) => <return true if equal>);