Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Ionic 2 : Set interval

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.

To explain :

My interval setting :

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

And my function in the same ts file :

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 ?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

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

about 4 years ago · Santiago Trujillo Relatório

0

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

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

100% working.

about 4 years ago · Santiago Trujillo Relatório

0

try this:

setInterval(this.refreshData, 300);
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda