我有一个功能,应该是秒表的基础。它为不同的方法返回一些值。我在此处使用可观察对象的订阅,并且想在计时器停止时取消订阅,但是它返回错误“ TypeError:this.customIntervalObservable.unsubscribe不是函数”
可能是什么问题,我该如何解决?
我的可观察代码:
customIntervalObservable = Observable.create((observer) => {
let count = 0;
setInterval(() => {
observer.next(count);
count = count + this.deg;
}, 1000);
});
我的方法代码是:
stopWatch(isSubscribed) {
if (isSubscribed) {
this.customIntervalObservable.subscribe((sec) => {
this.ss = sec;
this.getSeconds(this.ss);
if (this.ss / (60 * this.deg) === 1) {
this.ss = 0;
this.mm = this.mm + 6;
this.getMinutes(this.mm);
if (this.mm / (60 * this.deg) === 1) {
this.mm = 0;
this.hh = this.hh + 6;
this.getHours(this.hh);
}
}
});
} else {
this.customIntervalObservable.unsubscribe();
}
}
答案 0 :(得分:2)
您不能取消订阅square
,而只能取消订阅const array = [
{name: "TV", price: 12323, id: 321 },
{name: "Kettle", price: 123, id: 1211 },
{name: "TV", price: 3434, id: 3434312 },
{name: "Car", price: 12343, id: 123123123 },
{name: "TV", price: 4554, id: 2313123123311 }];
const hash = {};
array.forEach(function(obj){
if(!hash[obj.name])
hash[obj.name] = 1;
obj.name = `${obj.name}(${hash[obj.name]++})`;
})
console.log(array)
。
首先,获取对该订阅的引用:
Observable
然后,在else路径中,您可以编写:
Subscription