How can we get a countdown to run (in seconds), along with text, after a button is pressed?

158 views
Skip to first unread message

dave finney

unread,
Apr 17, 2016, 8:30:31 PM4/17/16
to DroidScript
Hi All,
   I've programmed a few PIC chips but this is my 1st attempt at a MP app.
   I've followed the guide and managed to get text boxes and buttons, but unsure how to proceed.

   I want a large button (perhaps 1/2 the screen) and, when pressed, I want text to appear on the left, and a countdown to start on the right.
   When pressed again, different text on the left, and a different countdown to start on the right

eg:
   egg   160
 toast   210
shave   198

etc

   The countdown will count in seconds down to zero, making a noise at 10 seconds before zero.

   I expect I need 2 text boxes (1 for the text and the other for the countdown) and a transparent button to go over them.
   I've made the above but I can't work out how to update the text in the boxes, or how to get a countdown to work
   I also can't work out how to make the button transparent.

   Any ideas to help would be appreciated, thanks,
      Dave.

Steve Garman

unread,
Apr 18, 2016, 4:57:59 AM4/18/16
to DroidScript
displaying a countdown timer that is anything like accurate in JavaScript can be a bit of a challenge.

Quite some time ago, Jorge produced a class that might make a useful starting point for you.

https://groups.google.com/d/msg/androidscript/aVYj-LS9O4g/keLpDaD1t5cJ

Dave Smart

unread,
Apr 18, 2016, 7:50:23 AM4/18/16
to DroidScript
You need to use the txt.SetText() method to change the text in a text control.

You can use the standard JavaScript setInterval method to repeatedly call a function so that you can update the timer values.

dave finney

unread,
Apr 20, 2016, 9:03:27 PM4/20/16
to DroidScript
Thanks Dave, txt.SetText() works.

Thanks Steve, link is useful.

Are all the commands in the docs? There are many I can't find. eg I copied "startTime = Math.floor(new Date().getTime()/1000);" from the linked countdown. Clearly it gets the current time and I found it gave a large number that increments once per second. My countdown can be done using that but I can't find the parts of that command in the docs.

Also, I defined a variable at the start: "var number2 = 2;" but, when I try to display the variable in a function, it says the variable (number2) is undefined. Are variables not transferred to functions?

Steve Garman

unread,
Apr 20, 2016, 11:04:26 PM4/20/16
to DroidScript
Much of the code used in DroidScript is JavaScript.

The Math and Date functions are not specific to DroidScript, you can find them in the docs by looking in the JavaScript reference.
The following links are to the online version of that reference.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

With regard to your number2 variable problem, if you declare a variable outside any function, it will be global and should be available from anywhere in the app.

If you declare it inside a function (including the OnStart function) it will be local, so you can only access it inside that function.

For more detail see http://www.w3schools.com/js/js_scope.asp

dave finney

unread,
Apr 30, 2016, 7:27:58 PM4/30/16
to DroidScript
Thanks Steve,

I'm making some progress and I now have a working countdown timer. Your links were a great help.

How can text within a textbox be centred (both V + H)? Currently, the text is at the top of the box. And can the text be set to autosize to fit the space available?

I've looked at the JR timer linked. I don't understand it but it seems to be based on Date.now(). I used this for my project where the date is stored and 1000 added (1 second). A SetInterval then checks the date regularly and, if greater than the stored time, a function is called. The function includes adding another second to the stored date. Something like:

function OnStart()
{
write pages here
timeNext = Date.now() + 1000;
}

setInterval (countdownupdate, 100);

function countdownupdate()
{
if ( timeNext < Date.now() )
{
timeNext = timeNext + 1000;
write countdown update here
}
}

ps, the problem with var number2 = 2 seems to be that you can't use "number" as a name for a variable

Steve Garman

unread,
May 1, 2016, 12:57:15 PM5/1/16
to DroidScript
number2 should be a perfectly acceptable name for a variable.
Reply all
Reply to author
Forward
0 new messages