Playing With Timers In Livecode

In this article we will talk about time handling in Livecode. We will create a simple application in which we will need to handle events generated at intervals, we will solve this problem in two different ways, the first taking advantage of the original Livecode functionalities, in the second way we will use a widget control written in Livecode Builder (LCB) called lcb_timer.

 

Problem

Write a program that shows the time including the seconds

 

Solution 1 - Using the Livecode's own functionalities

 

Interface

We will create the interface in a very simple way by creating a new stack and adding a label field which we will call “time” and a button that we will call “start” to start our clock.

Simple clock ui

With our timer’s GUI created, we can now write some code for it. We need to create an action that gets called when the start button is clicked. This handler will call himself every one second after called first time.

 

on updateTime

       put the long time into field "time"

       send "updateTime" to me in 1 second

end updateTime

 

Then we must call “updateTime” manipulator to start the process and we will have our simple clock working:

on mouseup

updateTime

end mouseup

This is the result:

Simple clock ui

Solution 2 - Using an LCB widget

 

For this solution we will use a control called lcb_timer. You can download and install this widget from our website (link at the bottom of this page)

 

Interface:

This time, we will create a label field named “time” and use a timer control (drag it from the tool palette once installed).

Simple clock ui

Now we will edit the code of the Timer control and put the following script:

on timer

put the long time into field "time"

end timer

 

Ready!

Simple clock ui

 

As we can see when selecting “Browse tool” the “Timer” control disappears, and reappears when changing back to “Pointer tool”, also the event generator starts automatically when switching to “Browse tool”. Only with 3 lines we have made a clock that shows us the time! This widget generates the time event every time the interval declared in the Interval property in milliseconds is fulfilled (by default 1000 = 1s) as long as "Browse tool" is selected, that is, the stack is in execution mode.

 

As we have seen previously, Livecode has functionalities for managing scheduled events but when several timers must be managed at the same time, a considerable load is generated on the message path, in addition, situations can be faced in which messages are pending and must be added. Extra checks to avoid such situations. Using the timer control we can provide a very simple solution to problems that can become a real headache in highly complex projects in which you need to periodically update the interface of your application, synchronize data, create animations or schedule events.

 


0 Comments

Currently, there are no comment.

Login to comment

FerrusLogic is it!

Payeer