无法停止观察间隔

时间:2017-10-05 19:04:56

标签: 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');
  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);

在我的ngOnDestroy

this.moduleProps.intervals.forEach((i)=>{
  i.complete();
});

但是在这之后我仍然看到控制台日志声明说"推送新项目"好像它还在我的间隔里运行。

2 个答案:

答案 0 :(得分:0)

根据此文档(https://www.learnrxjs.io/operators/filtering/takeuntil.html),takeUntil会一直发出,直到提供的observable发出。你正在完成观察而不会发出任何东西。尝试将.next()添加到onDestroy方法中。

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

这是一个有效的插件(https://plnkr.co/edit/Zdf8C9d8vHk84uIMYHvH?p=preview

答案 1 :(得分:0)

尝试使用package com.example.darkt.makeyouself.activities class FBLogin : AppCompatActivity() { private var binding: ActivityFbloginBinding? = null private var auth: FirebaseAuth? = null private var dbHelper: FirebaseHelper? =null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_fblogin) auth = FirebaseAuth.getInstance() dbHelper = FirebaseHelper() setContentView(R.layout.activity_fblogin) binding?.executePendingBindings() signIn.setOnClickListener{_ -> showSignInActivity()} } private fun loginToSystem() { val email = binding?.userName?.text.toString().trim() val password = binding?.userPass?.text.toString() binding?.executePendingBindings() Toast.makeText(this, email, Toast.LENGTH_SHORT).show() Toast.makeText(this, password, Toast.LENGTH_SHORT).show() } } 代替.takeWhile,它可以使用布尔值。

.takeUntil

OnDestroy只是private finished = false; // component property var interval = Observable.interval(this.settings.timing * 1000); var subscription = interval .takeWhile(_ => this.finished) .subscribe(t => { ...