Sleep procedure with system time behaves erratically


I have programmed a sleep procedure using system time and a do-nothing while loop. The procedure accepts one parameter which is time in milliseconds. The target time is calculated by adding the time interval to the current time. The while-loop runs until the system time exceeds the target time and then exits.

This worked in the Mole Mash game, to suspend the animated character's (i.e. mole's) activity for the specified time interval when touched by the player. However, it behaves erratically in a countdown timer where I use the same procedure inside a while-loop to print a number until the countdown reaches 0. There is no change to the text label that displays the countdown.

What am I missing?

Please note, I have implemented the countdown timer using the Clock's timer event and it works correctly. I do not need help implementing a countdown timer. There is some flaw in my understanding with the sleep procedure behaving differently than I expected and I am grateful for any help correcting it.

Thank you.

See the articles at

to increase your understanding.

I posted this question because the program's behavior I am observing does not align with what is described. Or maybe I am spooked. :slight_smile:

Here it says:

The main idea is that all actions that affect the UI happen sequentially . Some examples of these kind of actions could be changing the text of a label, or handling users interacting with the app via a button or a text box.

Sequential execution in App Inventor means that different blocks can never interrupt each other .

Different blocks and handlers will always be executed in sequence. Once a block starts executing, it will execute till the end, no matter if there are..

I interpreted sequential execution to mean that my code-block (below) should increment and display the score before the sleep procedure kicks in. However, the reverse happens - the sleep procedure kicks in and the text label shows the new score afterwards.

The sequence of execution should be:

  1. Increment score
  2. Update display label with new score
  3. Play sound and vibrate phone
  4. Sleep for n ms.

But what I observe is:

  1. Sleep for n ms
  2. Display the updated score
  3. Play sound and vibrate phone.

Does that make sense? I have included a link to my AIA file on GoogleDrive just in case you want to reproduce this behavior. The sleep4n procedure uses a timer event for waiting and behaves responsibly. :slight_smile: You can disable one and enable the other to compare and contrast the behavior.

Thanks for your time.

Why is a wait procedure generally a bad idea?
The model of event processing in App Inventor by Lyn

you might want to use the Please Wait method by Scott


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.