2

I have an Ionic 2 app, I want to launch a model every 10 minutes. In app.components.ts

I have simple function to launch this model.

openUpgradeModel() {
let modal = this.modalCtrl.create(UpgradeToFullVersionModel);
modal.present();
}

How can I get that function to launch every 10 minutes or so?

Update, so I tried using an Observable.timer, however it loops over and over.

  this.platform.ready().then(() => {
    let timer = Observable.timer(2000,1000);
    timer.subscribe(t=> {
   UpgradeToFullVersionModel();
});

Is there anyway to tell if the model is already open don't fire?

limit
  • 647
  • 2
  • 8
  • 27

2 Answers2

2

You need setInterval()

setInterval(() => { openUpgradeModal(); }, 1000 * 60 * 10);

https://stackoverflow.com/a/35829004/3221120

Community
  • 1
  • 1
monur00006
  • 47
  • 1
  • 6
1

Is there anyway to tell if the model is already open don't fire?

You can create a property in app.component.ts file

// Should be set to true the first time
private shouldShowModal: boolean = true;

Then set that property to false when showing the modal, but set it to true again when dismissing the modal:

openUpgradeModel() {
  let modal = this.modalCtrl.create(UpgradeToFullVersionModel);

  modal.onDidDismiss(() => {
    this.shouldShowModal = true;
  });

  this.shouldShowModal = false;

  // Show the modal
  modal.present();
}

And then, only show the modal if it's not being shown already:

this.platform.ready().then(() => {

  let timer = Observable.timer(2000,1000);
  timer.subscribe(t => {
    if(this.shouldShowModal) {
      this.openUpgradeModel();
    }
  });

//..
});
sebaferreras
  • 44,206
  • 11
  • 116
  • 134