Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Ionic 2: Intervalo establecido

Trato de establecer un intervalo en un archivo .ts pero no entiendo cómo usar una función en el mismo archivo en el intervalo.

Para explicar :

Mi ajuste de intervalo:

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

Y mi función en el mismo archivo ts:

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

Cuando ejecuto en mi dispositivo, tengo este 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)

Intento de esta manera pero no funciona:

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

Esto llama a mi función solo una vez.

Alguien tiene una idea?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

usar la función de flecha

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

o almacenar contexto como este

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

o usando bind

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

si solo una llamada de función:

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

puede obtener más información sobre esto con https://github.com/getify/You-Dont-Know-JS/tree/1st-ed/this%20%26%20object%20prototypes/ch1.md

about 4 years ago · Santiago Trujillo Report

0

Debe usar setInterval() con => flecha, como mencioné a continuación

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

100% funcionando.

about 4 years ago · Santiago Trujillo Report

0

prueba esto:

 setInterval(this.refreshData, 300);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!