Infinite while loop stalling out app

What I’m trying to do: I am trying to use a while loop so that only part of the function will run until certain criteria is met (this is for a quiz in my website which will progress when a confirm button is pressed)

What I’ve tried and what’s not working: I tried setting a variable, splitting the quiz into several parts that run on certain events, and altering the properties of other objects as an indicator (featured in the code sample below). All of these worked while in the anvil environment, however but upon opening the actual website the website freezes once the while loop begins to run.

Code Sample:

self.quiz1indicator.visible = True
  
while self.quiz1indicator.visible:
  pass

self.quiz1indicator.visible = True

Welcome to the forum!

You typically don’t want a busy loop like that in a web app, since you’re not giving the browser a chance to process events inside it.

Most often I’d keep a variable on the form that told me what the current state is, and use that in my button press function to know which section of code should execute.

If you really must use a while loop like that, you need to give control back to the browser inside it using time.sleep(0) or the tab will freeze.

3 Likes

Thank you so much! It worked perfectly.

I like to use a timer to check the condition every 1 or 0.1 (or whatever you like) seconds.

2 Likes