Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Can';不停可观测区间_Angular_Rxjs_Intervals - Fatal编程技术网

Angular Can';不停可观测区间

Angular Can';不停可观测区间,angular,rxjs,intervals,Angular,Rxjs,Intervals,我已经尝试了我所能找到的一切,但似乎无法让这个间隔停止在订阅中执行代码。我是不是用错了时间间隔 var subject = new Subject(); var interval = Observable.interval(this.settings.timing * 1000); var subscription = interval .takeUntil(subject) .subscribe(t => { console.log('pushing new item');

我已经尝试了我所能找到的一切,但似乎无法让这个间隔停止在订阅中执行代码。我是不是用错了时间间隔

var subject = new Subject();
var interval = Observable.interval(this.settings.timing * 1000);
var subscription = interval
  .takeUntil(subject)
  .subscribe(t => {
  console.log('pushing new item');
  this.activeItems[0].contentLeaving = true;
  setTimeout(()=>{
    this.activeItems[0].imageLeaving = true;
    setTimeout(()=>{
      this.activeItems.push(this.activeItems.shift());
      setTimeout(()=>{
        this.activeItems[1].contentLeaving = false;
        this.activeItems[1].imageLeaving = false;
      },510);
    },510);
  },510);
});
this.moduleProps.intervals.push(subject);
在my
Ngondestory中

this.moduleProps.intervals.forEach((i)=>{
  i.complete();
});
但是在这种情况发生之后,我仍然看到控制台日志语句说“pusingnewitem”,就好像它仍然在我的时间间隔内运行一样。

根据本文档(),takeUntil发射,直到提供的可观察发射为止。你正在完成可观察的,没有发出任何东西。尝试将.next()添加到onDestroy方法中

this.moduleProps.intervals.forEach((i)=>{
  i.next(); // have subject emit
  i.complete();
});
这是一个工作plunk()

根据本文档(),TakeTill发射,直到提供的可观察发射。你正在完成可观察的,没有发出任何东西。尝试将.next()添加到onDestroy方法中

this.moduleProps.intervals.forEach((i)=>{
  i.next(); // have subject emit
  i.complete();
});

这是一个工作plunk()

尝试
.takeWhile
而不是
.takeUntil
,它可以使用布尔值

private finished = false;  // component property

var interval = Observable.interval(this.settings.timing * 1000);
var subscription = interval
  .takeWhile(_ => this.finished)
  .subscribe(t => {
  ...

OnDestroy就是this.finished=true

尝试
.takeWhile
而不是
.takeUntil
,它可以使用布尔值

private finished = false;  // component property

var interval = Observable.interval(this.settings.timing * 1000);
var subscription = interval
  .takeWhile(_ => this.finished)
  .subscribe(t => {
  ...

OnDestroy就是this.finished=true

我也试过,还有
.next()
传回一些东西,间隔就会继续。@Jordan,你能把你的间隔订阅存储起来吗?然后在你的ngOnDestroy中取消订阅以停止间隔。在我也尝试过之前,不要使用take,它也不起作用。有趣的是,如果我尝试在超时设置后立即调用
.next()
.compelte()
,这一切都会很好地工作。只是当我稍后尝试使用引用时,它不起作用one@Jordan你确定Ngondestory方法被触发了吗?我也试过了,还有
.next()
传回一些东西,间隔就会继续。@Jordan,你能不能只存储你的间隔订阅,然后在您的Ngondestory中取消订阅,以停止间隔。在我也尝试过之前,不要使用take,它也不起作用。有趣的是,如果我尝试在超时设置后立即调用
.next()
.compelte()
,这一切都会很好地工作。只是当我稍后尝试使用引用时,它不起作用one@Jordan您确定正在激发Ngondestory方法吗?您可以在
subscribe()
内部调用
subscribe.unsubscribe()
。或者我不明白你想做什么…你可以调用
subscription.unsubscribe()
inside
subscripte()
。或者我不明白你想做什么…takeWhile接受一个假定返回布尔值的函数,而不仅仅是booleantakeWhile接受一个假定返回布尔值的函数,而不仅仅是布尔值