Countdown Timer

Post Reply
User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Countdown Timer

Post by rbytes » Wed Aug 23, 2017 4:21 am

While this countdown timer does work, it is not easy to read the display. I look forward to the time when font size (and font name) are available as SPL commands.
But it should be easy to tell when the countdown reaches 0. The screen turns red.

Code: Select all

''
  Countdown Timer by rbytes
  August 2017
  Set s to the desired number of seconds
''
x=-1                  'actually x can be set to any non-zero value
s=10                  'number of seconds to count down
t = #.textbox         'textbox "t" is created
t.text=""
t.edit=0
t.height=40
t.width=60
t.x=100
t.y=100
#.show(t)             'textbox "t" is shown on the screen
#.drawrect(95,95,150,140)  'draw a box around the textbox
>
y = #.int(#.time())   'number of elapsed seconds
? y!=x                'if second has changed
  s-=1                'subtract 1 from elapsed time
  '#.output(y)
  t.text = s          'update text of textbox "t" to value s (seconds remaining)
  ? s=0               'if timer has run out
    #.scrclear(1,0,0) 'turn screen red
    #.drawrect(95,95,150,140)  'draw the box again
    <<                'break
  .
  x=y                 'store the current second to compare y to it
.
<
Screenshot (10).png
Screenshot (10).png (31.85 KiB) Viewed 420 times
Zzzzz

User avatar
Mr. Kibernetik
Site Admin
Posts: 4607
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPad, iPhone, MacBook
Location: Russia
Flag: Russia

Re: Countdown Timer

Post by Mr. Kibernetik » Wed Aug 23, 2017 5:54 am

Inspired by your timer, I also made a timer:

Code: Select all

maxt = 10 'in seconds
#.scroff()
>
  t = #.time()
  ? t>maxt, t = maxt
  #.scrclear()
  #.drawtext(10,10,gett(t))
  #.drawtext(10,40,gett(maxt-t))
  #.scr()
< t<maxt

gett(t)=
  hr = #.lower(t/3600)
  min = #.lower((t-hr*3600)/60)
  sec = t%60
  <= #.str(hr,"00:")+#.str(min,"00:")+#.str(sec,"00.00")
.
Снимок.PNG
Снимок.PNG (1.48 KiB) Viewed 415 times

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Countdown Timer

Post by rbytes » Wed Aug 23, 2017 2:02 pm

Nice! I see there are much more efficient ways to code with SPL.

Is there a way to derive year, month and day from the #.time() function?
Zzzzz

User avatar
Mr. Kibernetik
Site Admin
Posts: 4607
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPad, iPhone, MacBook
Location: Russia
Flag: Russia

Re: Countdown Timer

Post by Mr. Kibernetik » Wed Aug 23, 2017 2:10 pm

rbytes wrote:
Wed Aug 23, 2017 2:02 pm
Is there a way to derive year, month and day from the #.time() function?
No, #.time is just a timer (note that there can be unlimited amount of named timers using #.time function).
There will be dedicated date/time functions in SPL.

Your wish list sorted by priority is very welcome!
Also I found rosettacode.org to be very useful to find interesting tasks to facilitate development of SPL function set.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4607
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPad, iPhone, MacBook
Location: Russia
Flag: Russia

Re: Countdown Timer

Post by Mr. Kibernetik » Wed Aug 23, 2017 4:46 pm

Date function #.today and time function #.now will appear in version 0.0.12.
This is the code syntax example:

Code: Select all

#.scroff()
>
  #.scrclear()

  h,m,s = #.now() 'time
  t = #.str(h,"00:")+#.str(m,"00:")+#.str(s,"00.000")
  #.drawtext(10,10,t)

  d,m,y = #.today() 'date
  t = #.str(d,"00'.'")+#.str(m,"00'.'")+#.str(y)
  #.drawtext(10,40,t)

  #.scr()
<
And the output:
Снимок.PNG
Снимок.PNG (1.87 KiB) Viewed 401 times

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Countdown Timer

Post by rbytes » Wed Aug 23, 2017 5:22 pm

Valuable addition! Thanks!
Zzzzz

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Countdown Timer

Post by rbytes » Sat Aug 26, 2017 1:50 pm

I updated my SPL version last night, and this code is now working well.
Zzzzz

Post Reply