Clock

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

Post Reply
Squirly
Posts: 5
Joined: Tue Aug 08, 2006 10:33 am

Clock

Post by Squirly » Thu Oct 26, 2006 9:15 am

Hi, I'm new. :)

I know this doesn't exactly fit the forum , but I'm frustrated to all hell and there doesn't seem to be anywhere else.

I'm looking for a clock. Specifically, a simple timer which gets date and time and displays it in a label, updating automatically every second. Simple right? That's what I thought, until I realized that if I don't have "wait 1 second with messages" in the loop, it'll just take up all the cycles and won't let you do anything. However, if I do include this line it'll run fine...for a while. It doesn't seem to follow any particular pattern but every once in a while my program will just stop responding. Killing the process (Ctrl . ) will highlight "wait 1 second with messages".

In other words it's flawed but nothing else seems to work. I've looked for a clock/timer function all over the net...so far nix.

Help?

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Thu Oct 26, 2006 9:25 am

Hi squirly,

try switching to a send in time construct:

Code: Select all


-- you need a stack with a button and a field called "myField"
-- in the button
-- simple clock

on mouseUp
  set the flag of me to not the flag of me
  if the flag of me then startTimer
end mouseUp

on startTimer
  put the long time into field "myField"
  if the flag of me then send startTimer to me in 1 second
end startTimer

You can now start and stop the clock by clicking on the button.

Hope this helps,

Malte

PS:
If you want to learn more about timers and a lot of other stuff, you might want to check out the free eBook I wrote:

http://www.derbrill.de/tutorials_e.html

All the best,

Malte

Squirly
Posts: 5
Joined: Tue Aug 08, 2006 10:33 am

Post by Squirly » Thu Oct 26, 2006 10:34 am

I'll try it out. Thanks!

Squirly
Posts: 5
Joined: Tue Aug 08, 2006 10:33 am

Post by Squirly » Thu Oct 26, 2006 11:05 am

Well, turns out I need to be constantly running, while I'm doning whatever else needs doing. In other words, I've got it in a repeat forever loop (hence the "wait 1 second with messages" line). Is this the best way or can you think of something better?

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Thu Oct 26, 2006 11:10 am

I strongly recommend to use a timer as described above. You can make it constantly running by calling the timer on openCard / openstack.

In the card script:

Code: Select all

on openCard
  set the timerFlag of this stack to true
  startTimer
  pass openCard
end openCard

on startTimer
  put the long time into fld "myField"
  if the timerFlag of this stack then send startTimer to me in 1 second
end startTimer

on closeCard
  set the timerFlag of this stack to false
  repeat for each line theLine in the pendingmessages
  if "startTimer" is in theLine then cancel item 1 of theLine
  pass closeCard
end closeCard
This way it will start running when the card is opened and stop when you leave the card.

Hope this helps,

Malte

Post Reply

Return to “Converting to LiveCode”