Help with call a function inside setInterval()


Hello, I am developing a app with ionic 2 but I have a problem. I don’t know how to call a function inside setInterval() because the app does not recognise the constructor when I try it. I have this:

 var timer= setInterval(this.obtenerPosicion,5000);
 var timer2= setInterval(this.enviarPosicion,5000);

  if (typeof this.latitude != 'undefined' && typeof this.longitude != 'undefined')
    var token=this.token;
    var id_usuario=this.id_usuario;
    var headers= new Headers();
    console.log('latitud: '+this.latitude);
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization', 'Token e31e642fa2aa05743309a9a4deef815302c6c287');'',datos,{
    }).subscribe(success =>{


The problem is that the app does not recognise, but when I try to call enviarPosicion outside of setInterval() It runs correctly.

Here is de complete code:

Anyone knows how to fit it?


You are being bitten by the fact that ‘this’ in JavaScript/TypeScript is weird.


Thank you very much, I didn’t know it. It runs now.


This link might help solve the issue with using setTimeout and setInterval methods in Ionic 2 applications:

setTimeout and setInterval methods: Ionic 2