Creating a Countdown Timer

Hi All,

i hate to be a person who asks for full help, but am a little stuck and am quite new to XOJO……

How do you get a timer to show text that counts down from a value.

For example:

dim countdowntime as integer

countdowntime  = val(Textinputbox.text)

// PASS This to a timer that updates a label text counting down from the value in the Textinout box (in Mins)

E…g. I place the number 4 in the input box, i want to get a countdown from the number 4 to 0 showing in the label text….

really really appreciate any help on this

Drag a Timer from the Library to your window. Add an “Action” event to your Timer. In the code for the Action event:

[code]dim countdowntime as integer

countdowntime = val(Textinputbox.text)
countdowntime = max(0, countdowntime - 1)
Textinputbox.text = str(countdowntime, “0”)[/code]

Set the period of your Timer to 1000. Set it’s mode to multiple (2).

There are actually a lot of ways to attack your question, depending on how accurate you need the timer to be, if you want it to do some action when it gets to zero, if you’d prefer to instantiate the Timer in code and keep as a property of the window rather than a control, etc.

ok … update… I’ve don’e a little bit more looking into it:

This is what i have off a submit button.

I will replace the 9 with the minutes being passed into it:

The issue i now have is the above is counting in seconds rather than minutes ( i need to do both)

e…g

i pas 3 mins, 3 mins gets to 60 seconds and then i expect it to count down for 60 seconds to 0….

You’re trying to do it synchronously. All that’s going to do is hang up your app until the for loop completes in roughly 10 seconds. You need to do your countdown asynchronously to get it to work how you want. See my reply above.

Hi Brad,

Yep you are right, kills my app :frowning: Sorry, i didn’t see your post above until i logged out and back into XOJO forums :frowning:

Will give the above a bash now…

Hey Brad,

Really appreciate the help on this one! Could you help me a little more please?

I really need it to count down from minutes then down to seconds. The value given in the text field for example if 2 mins, i really need it to show: 1:12 (1min, 12 seconds)…

Also, if i do the following:

so take value from 1 and countdown in another, the countdown doesn’t run…

The count down should run if you set the period of the Timer to 1000 (ms) and the mode to multiple (2).

If you want to show minutes and seconds, create a property in your window, countdowntime as integer. Set it’s default value to 120 (i.e. 120 seconds). Then adjust the Timer.Action code as follows:

[code]dim mins, secs as integer

// self is not needed here, but reminds you it’s a window property
self.countdowntime = self.countdowntime - 1
mins = self.countdowntime \ 60
secs = self.countdowntime mod 60
TextField4.text = str(mins, “0”) + “:” + str(secs, “00”)[/code]

HTH.

Hey Brad,

Thanks once again, i really appreciate the help you are giving me here. when i add the number to count down from in text field 1

textfiled1 - 10 mins

then press the submit button, which launches the above code

text filed 4 - states 2 mins and counts down.

I assume this is due to the Property being set at 120?

How do i feed the integer from textfield1 into the method above so it counts down from there.

Really sorry for the Noob questions, really new to all this stuff. I am reading a few books but am a bit of a slow learner… :frowning:

Yes, the 120 makes it so the timer takes 2 minutes to count down since 2 minutes=120 seconds.

In your submit button, you should put:

self.countdowntime=val(inputTextfield.text) 'time in seconds 

and the timer will count down the time specified in seconds. If you want to be able to enter 5:30 to mean 5 minutes and 30 seconds, that will be a bit more complex, but we can show you how.

Also, all of us were noobs at one point or another so don’t worry to much about the questions.

[quote=46991:@Jay maxted]How do i feed the integer from textfield1 into the method above so it counts down from there.

Really sorry for the Noob questions, really new to all this stuff. I am reading a few books but am a bit of a slow learner… :frowning:
[/quote]

You could put the number of seconds into the text field, e.g. 120. Then create an Open event handler on the window, and put this code in there:

self. countdowntime = val(TextField4.text)

Noobz are cool. Glad you’re here.

Hi both,

Thanks for the help

@Jason King - yes please. The main input is minutes so really need it to count down in mins and seconds…

Again, really appreciate the help

Here is the code that would allow you to enter a time like 5:30 and have it properly register, note that this doesn’t work for hours but could be easily expanded if you needed it to work for hours also.

//IMPORTANT: The time must be entered with minutes and seconds for this to work, otherwise an //OutOfBoundException will be raised, e.g. 4 minutes would be entered as 4:00 dim minutes, seconds, totaltimeinseconds as integer dim thetime, tmpstring() as string thetime=TextField4.Text tmpstring=thetime.Split(":") minutes=val(tmpstring(0)) seconds=val(tmpstring(1)) totaltimeinseconds=(60*minutes)+seconds self.countdowntime=totaltimeinseconds

The code takes the string entered into the textfield and splits it at the “:” using the split function. Since the split function returns an array of strings, a temporary array, tmpstring(), is created to hold the values. Then the indexes of the values in the array are used to retrieve the values in the array which are converted into integer. Since, the countdowntime variable stores the time in seconds, then the minutes are converted into seconds. Finally, the time entered is stored in the countdowntime variable to be used by the timer.

hey @Jason King

thanks for the above.

i have copied the above into my timer and commented out the code that i was using.

I have then loaded my app

In field one i have typed 4:00

i have then clicked the submit button

This enables the timer and executes the code provided

When the code is run it then fails on:

seconds=val(tmpstring(1))

i get an out of bounds exception…

** Note, if i only put 20 then i get an out of bounds on ( minutes=val(tmpstring(0)) ) - which is what you comment said

    • I am expecting to see the count down start and run then in textfield4

The code should be put into the submit button that you use to initialize the timer. If it is in the action event of the timer, then it will fail. Here is my quick example. It is probably better to have the time remaining shown in another textfield or a label so that you can change the time you entered and not have it constantly updated by the timer to prevent you from changing the value.

@Jason King @Brad Hutchings thank you both very much, its working as its meant to now. Really appreciated your help

You’re welcome. If you have any other questions feel free to ask! :slight_smile:

Hi @Brad Hutchings and @Jason King

I do have another question. I want to have a hidden text filed that where i have a static 10 second (00:10) set, i want to be able to add these the main time (which we have done above) to this.

I have tried a few ways now but seem to be coming up short.

Firstly i did this:

dim i as integer
BothAdded.text=str(val(Minutes.text)+val(Hidden.text))

The above was run off a submit button. All i kept getting back was 1 . I tried playing with the static value, setting as 0.10 / 00.10 / even 1:10. But each time i added them i either got 1 or 2 - So i can only assume its rounding it up?

Are you able to shed any light on this for me please….

Are you trying to make it so that when you click a button a certain amount of time (10 seconds) is added to the time in the timer? I’m not exactly sure of what you are trying to accomplish.

Hey jason,

Yep im taking the value we have created and trying to add 10 seconds to it

Ok then that is easy enough. In the Action event of a NEW button add the following code:

countdowntime=countdowntime+10

That should do what you need. If you want to add more than 10 seconds, just change the value from 10 to something else.