2017-10-14 214 views

回答

0

试试这个。我认为这是一个范围问题。而不结合在setInterval的范围进入该窗口对象

 setInterval(function(){ this.myfunction();}.bind(this), 1000); 
3

的解决方案是使用Arrow functions

setInterval(() => { 
    this.myfuntion(); // Now the "this" still references the component 
}, 1000); 

当使用箭头功能,所述this属性不被覆盖,仍然引用组件实例。

1

基本上有两种方法来执行该操作。

尝试使用可以最适合您的要求的observable。

方法1:

import {Observable} from 'Rxjs/rx'; 
import { Subscription } from "rxjs/Subscription"; 

// if you want your code to work everytime even though you leave the page 
Observable.interval(1000).subscribe(()=>{ 
    this.functionYouWantToCall(); 
}); 

方法2:

// if you want your code to work only for this page 
//define this before constructor 
observableVar: Subscription; 

this.observableVar = Observable.interval(1000).subscribe(()=>{ 
    this.functionYouWantToCall(); 
}); 

ionViewDidLeave(){ 
    this.observableVar.unsubscribe(); 
}