How to set order of multiple Typescript functions


#1

Hello guys,

How can I set several functions happen in chronological order in typescript?
I have many functions which should not happen at a time but they’re all assigned to a single button.

For example, there are three functions linked to this button.

button01function() {

  const myModal = this.modal.create('ModalPage');
  myModal.present();

  this.insomnia.allowSleepAgain();

  this.unregisterBackButtonAction && this.unregisterBackButtonAction();

}

myModal.present() should happen first and then this.insomnia.allowSleepAgain(); should happen 2 seconds later. The last one should happen 5 seconds later.

How can I set this kind of timing on typescript function?

Thanks in advance!


#2

Construct a dismiss handler for your modal, so you can say
myModal.onDidDismiss(_ => doStuff);
Don’t present the modal until you set that. It might feel backwards the first time you do it, but you’re telling the page how you want it to terminate, so you need to tell it that before you create the page.

Then inside doStuff, you call the sleep function and use the NodeJS timer function to wait 5 seconds, then execute the rest. Depending on how your system is set up, you may need to install node typings. So I’d search NodeJS timer, and get that working first, because it might be the hardest to make happen.


#3

Thanks again!

The modal was a dummy example…
This seems to be working …

setTimeout(() => { }, 1000);
https://www.w3schools.com/jsref/met_win_settimeout.asp
I can set several time out within a typescript function.
I will look into NodeJS timer too!


#4

Take a look at the type of the setTimeout object.


#5

Yes:

https://nodejs.org/en/docs/guides/timers-in-node/

This works! am testing on an actual apk file now.


#6

Something to check. Can you declare a variable example: NodeJS.Timer ? If not, to fix that you need to download the node typings library wth npm. It doesn’t matter for simple timers, but if you ever want to pass a timer to a function, or do other fancy things, then it comes in handy. So you may not need it now, but be aware there might be a hidden error in your timer definitions if you program other things later.


#7

Nope, yeah I will need to install node typings library then.
This one I’m building now is relatively simple and it’s relying on media files inside rather than having lots of complicated features :sunglasses:
Thanks for everything!