我想每1秒运行一次函数。搜索后,我发现setInterval
,但它不适合我。我也试过this.myfuntion
但它不起作用。setInterval in IONIC 3
1
A
回答
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();
}
相关问题
- 1. ion-list vs ion-scroll vs virtualScroll in ionic 3
- 2. * ngIf in ion-option in ionic 2
- 3. .json in Ionic 2
- 4. PhoneRTC in Ionic
- 5. hockeyapp in ionic 2
- 6. Ionic 2 setInterval变量范围
- 7. SetInterval with javascript in javascript
- 8. fs.watch vs setInterval in node.js
- 9. Angularjs $ http post in Ionic
- 10. setState不工作setInterval In React
- 11. Ionic Cordova In App Purchase - 失败
- 12. Ionic 3 - Pan Events
- 13. IONIC 3 CORS ISSUE
- 14. ng-modal undefined in ionic framework
- 15. 圆形图像/轮廓Pic in Ionic 2/3
- 16. Http provider Error Ionic 3
- 17. ionic 3 alert font-sizing
- 18. Ionic 3 coverflow实现
- 19. Ionic 3 http请求
- 20. ionic 3 EmailValidator用法
- 21. 找不到module node_modules \ @ionic \ app-scripts IONIC 3
- 22. 3 datepick in 3 pages in 1 page
- 23. Google Auth in Ionic应用程序
- 24. Ionic 3 - FirebaseInstanceID找不到
- 25. UILongPressGestureRecognizer in Swift 3
- 26. jEditable in Rails 3
- 27. NameError in Rails 3
- 28. rindex in Swift 3
- 29. Ckeditor in Rails 3
- 30. ALAssetsLibraryGroupsEnumerationResultsBlock in swift 3