如何检测变量的变化?

骇人的

我在全局范围内有一个变量,需要定期检查是否有更改。这就是我将在简单的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章