I have a variable in global scope that I need to check periodically for changes. This is how I would do it in simple JS:
let currentValue, oldValue;
setInterval(()=>{
if(currentValue != oldValue){
doSomething();
}
}, 1000)
How is it done using Observables
?
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged();
Or you can optionally give a comparator-function:
Observable.interval(1000)
.map(() => currentValue)
.distinctUntilChanged((oldValue, newValue) => <return true if equal>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With