I always thought that the timer only fired when the form was displayed on the screen, but I found that it keeps firing:
- when the browser is minimized or another tab selected
- when another form is displayed (but it still exists in the cache in hashrouting) Edit: not True.
If you use your timer for polling the database for updates, this is probably not what you want.
Here is how you can make it perform its task only when the form is visible on the screen:
import anvil.js
class MyForm(MyFormTemplate):
def __init__(self, **properties):
self.init_components(**properties)
self._interval = self.timer.interval
def _is_app_hidden(self, **event_args):
# True if browser minimized or tab hidden
return anvil.js.window.document.hidden
def timer_tick(self, **event_args):
if not self._is_app_hidden():
# do your thing
pass
def form_show(self, **event_args):
# start timer
self.timer.interval = self._interval # Edit: not necessary
def form_hide(self, **event_args):
# stop timer
self.timer.interval = 0 # Edit: not necessary. The timer stops on hide.