Creating a Countdown Timer

Xojo R2.1 - Example to check a database or make a other process (xx) minutes.

Objetive: check new info in my database any 2 minutes

Timer Object


a) Put Timer object in your windows
b) Set Timer Properties Mode: Multiple Period: 1000

Window Definitions and object


a) put textfield1 objet in you windows panel
b) Rename textfield1 to TextFieldTime
c) Open a (action) in timer and write this:

dim mins, secs as integer
self.countdowntime = self.countdowntime - 1
mins = self.countdowntime \ 60
secs = self.countdowntime mod 60
TextFieldTime.text = str(mins, “0”) + “:” + str(secs, “00”)

’ ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
’ Control to reinit Timer o Schedule Action
’ ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
if TextFieldTime.text = “0:00” then

' Put Timer off
countdowntime  = 120 // Redefine Time
Timer1.Enabled = false // Deactive timer

’ ************
Call MyRoutineToCheckDatabase() //Check database new info
’ ***********
’ Put timer on
Timer1.Enabled = true
Timer1.Reset

end if

I’m lazy so I let the software do the work for me. For timers that are less then 24 hrs. More than, you’ll need to do the math on the SQL String

If you want to show a count down timer in a text field put a text field on the page. Put a timer (myTimer) on the page Period 1000 ModeOff

Put the time fields on the page i.e. one for hours, mins and seconds.
Put a button on the page.

Add myTime as String to the window.

Add a Method to the window call it TimerIsOn
Code:
Dim d As New Date
Dim s As String = "2000-01-01 " + myTime
d.SQLDate = s
d.Seconds = d.Seconds - 1
myTime = Right(d.SQLDateTime, 8)

If myTime = “00:00:00” then myTimer.Mode = Timer.ModeOff

TextField1.Text = myTime

In the pushbutton.Action

myTime = Format(val(textFieldHours.Text), “00”) + “:” + Format(val(textFieldMin.Text), “00”) + “:” + Format(val(textFieldSec.Text), “00”)

myTimer.Mode = Timer.ModeMultiple