创建后更改时间间隔/可观察的设置 [英] Change Interval/settings of observable after creation

查看:97
本文介绍了创建后更改时间间隔/可观察的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RxJS中,创建后如何更改间隔设置?

In RxJS how would I change the interval setting after creation?

到目前为止,我有这个,但它不起作用

So far I have this, but it doesn't work

var observable = Rx.Observable.interval(500)
  .map(function (data) { return "Hello World " + data; });

observable.subscribe(console.log);

setTimeout(function () {
  observable.interval(3000);
}, 3000);

上面写着"TypeError:observable.interval不是sixage.js:10:14的函数"

It says "TypeError: observable.interval is not a function at sixage.js:10:14"

jsbin

修改:

这是使用接受的答案后的最终产品.

This was the final product after using the accepted answer.

var intervalUpdateS = new Rx.Subject();
var observable = intervalUpdateS.startWith(500).flatMapLatest(function(intvl){
  return Rx.Observable.interval(intvl);
})
.map (function (data) { return "Hello World " + data; });

observable.subscribe(function (msg) {
  console.log(msg);
});

setTimeout(function () {
  intervalUpdateS.onNext(3000)
}, 3000);

jsbin

推荐答案

interval是在'class'Rx.Observable上定义的,而不是在原型级别上定义的,即,不是在Rx.Observable的每个实例上定义的.因此,在可观察实例上的observable.interval肯定会给您该错误.

interval is defined on the 'class' Rx.Observable, not at the prototype level, i.e. not on every instance of Rx.Observable. So observable.interval on an observable instance will definitely give you that error.

如果您是间隔修改的来源,那么我只能想到使用主题来推动您的修改.这样可以正常工作:

If you are the source of modification of the interval, I can only think of using a subject to push your modifications. This would work that way:

var intervalUpdateS = new Rx.Subject();
var observable = intervalUpdateS.flatMapLatest(function(intvl){
    return Rx.Observable.interval(intvl);
})
.map (function (data) { return "Hello World " + data; });

然后使用intervalUpdateS.onNext(newValue);

未经测试,但希望可以照常工作.

Haven't tested but hopefully should work as is.

关于主题: https://github. com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/subjects.md

关于flatMap: http://reactivex.io/documentation/operators/flatmap.html 为什么需要使用flatMap?

About flatMap : http://reactivex.io/documentation/operators/flatmap.html, and Why we need to use flatMap?

这篇关于创建后更改时间间隔/可观察的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆