0

I'm trying to access a property from a function but for some reason it gives an undefined error.

export class InserterComponent implements OnInit {
  public imagenActual: string = 'John';
  contador: number = 0;
  imagenes = IMAGENES;
  imagen: Imagen = {
    numero: 1,
    url: 'peter'
  }

Here's the interface imagenes

import { Imagen } from './imagen';

export const IMAGENES: Imagen[] = [
  { numero: 1, url: 'Dr Nice' },
  { numero: 2, url: 'Narco' },
  { numero: 3, url: 'Bombasto' },
  { numero: 4, url: 'Celeritas' },
  { numero: 5, url: 'Magneta' },
  { numero: 6, url: 'RubberMan' },
  { numero: 7, url: 'Dynama' },
  { numero: 8, url: 'Dr IQ' },
  { numero: 9, url: 'Magma' },
  { numero: 0, url: 'Tornado' }
];

And here's the function that gives the error (error appears at "this.imagenes"):

  public startSession() {
    setInterval(this.funct, 2000);
  };

  public funct() {
    this.imagenActual = this.imagenes[this.contador].url;
    setTimeout(alert, 50000);
    this.contador++;
  }
Blunderchips
  • 534
  • 4
  • 22
  • Have you tried to log `this.contador` value just before call `this.imagenActual = this.imagenes[this.contador].url;`? What do you read? – Giovanni Esposito Jul 07 '21 at 12:40
  • 1
    It read as undefined as well. Looks like I had a misconception about how "this" works on javascript and it doesn't work the same as c# and java, I'll have to look up some info. Thanks everyone – Layer891203 Jul 07 '21 at 12:45
  • Try this: setInterval(() => { this.funct() }, 2000); – MikeOne Jul 07 '21 at 12:51

0 Answers0