Function not found, but works in javascript

Hi all,

Usually this would work in regular javascript but its causing an issue in ionic/typescript and I can’t figure out why.

Error: function not found -> this.DriverFound()

  DriverFound() {
    this.isPickupActivated = true;
  }

  RequestPickup() {
    console.log("Pickup requested");
    this.isPickupRequested = false;
    
    let loader = this.loadingCtrl.create({
      content: "Searching for driver. . .",
      duration: 3000
    });
    loader.present();
    setTimeout(function(){ this.DriverFound(); }, 3000);
  }

this is different inside the setTimeout. Use an arrow function.

I am always suspicious when I see setTimeout(). It’s virtually never the best idea.

1 Like