typescript 离子 2 : 设置间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43490761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:28:08  来源:igfitidea点击:

Ionic 2 : Set interval

javascriptangulartypescriptionic2ionic3

提问by V. Pivet

I try to set an interval in a .ts file but I don't understand how to use a function in the same file in the interval.

我尝试在 .ts 文件中设置间隔,但我不明白如何在间隔中的同一文件中使用函数。

To explain :

解释 :

My interval setting :

我的间隔设置:

this.task = setInterval(function () {
            this.refreshData();
        }, 300);

And my function in the same ts file :

我在同一个 ts 文件中的功能:

refreshData() : void{
        console.log('update...');
    }

When I run on my device, I have this error :

当我在我的设备上运行时,出现以下错误:

04-19 10:38:57.535 21374-21374/com.ionicframework.app722890 I/chromium: [INFO:CONSOLE(79432)] "TypeError: this.refreshData is not a function
                                                                                      at file:///android_asset/www/build/main.js:10987:18
                                                                                      at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:10284)
                                                                                      at Object.onInvokeTask (file:///android_asset/www/build/main.js:39626:37)
                                                                                      at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:10220)
                                                                                      at e.runTask (file:///android_asset/www/build/polyfills.js:3:7637)
                                                                                      at invoke (file:///android_asset/www/build/polyfills.js:3:11397)
                                                                                      at e.args.(anonymous function) (file:///android_asset/www/build/polyfills.js:2:30193)", source: file:///android_asset/www/build/main.js (79432)

I try this way but I doesn't work :

我尝试这种方式,但我不工作:

this.task = setInterval(this.refreshData(), 300);

This call my function only one time.

这仅调用我的函数一次。

Someone have an idea ?

有人有想法吗?

回答by Tiep Phan

use arrow function

使用箭头函数

this.task = setInterval(() => {
  this.refreshData();
}, 300);

or store context like this

或像这样存储上下文

let self = this;
this.task = setInterval(function () {
  self.refreshData();
}, 300);

or using bind

或使用 bind

this.task = setInterval((function () {
  this.refreshData();
}).bind(this), 300);

if only one function call:

如果只有一个函数调用:

this.task = setInterval(this.refreshData.bind(this), 300);

you could learn more about this with https://github.com/getify/You-Dont-Know-JS/tree/1st-ed/this%20%26%20object%20prototypes/ch1.md

您可以通过https://github.com/getify/You-Dont-Know-JS/tree/1st-ed/this%20%26%20object%20prototypes/ch1.md了解更多相关信息

回答by user3563484

You should use setInterval() with => arrow, as I mentioned below

你应该使用 setInterval() 和 => 箭头,正如我在下面提到的

   setInterval(() => {      
          console.log('timer');
          //you can call function here
    },5000);

100% working.

100% 工作。

回答by flowest

try this:

试试这个:

setInterval(this.refreshData, 300);

回答by Sagar Kulkarni

I copy pasted your code in my app.

我将您的代码复制粘贴到我的应用中。

This worked:

这有效:

this.task = setInterval( () => {
  this.refreshData();
}, 300);