Repeat Timer

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Repeat Timer

Post by ace16vitamine » Tue May 22, 2018 2:30 pm

Dear all,

I need a simple counter to send every 30 Sec. emails (dosendmail) from a database query (dostartreading).

The think is: After 30 Sec. the counter is over and it will not repeat . :roll:

Any ideas how to repeat the counter, if currentTimerValue = 0 ?

Thanks
Stef




on declare_timer
global pTime
put 30 into pTime
put pTime into fld "fldCount"
countDownTimer pTime
end declare_timer

on countDownTimer currentTimerValue
subtract 1 from currentTimerValue
put currentTimerValue into fld "fldCount"
if currentTimerValue > 0 then
send "countDownTimer" && currentTimerValue to me in 1 sec

end if

if currentTimerValue = 0 then
dostartreading
global tRecords, tRecords2
put tRecords into field "Data"
dosendmail

end if


end countDownTimer

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Repeat Timer

Post by dunbarx » Tue May 22, 2018 4:07 pm

Hi.

I guess you want a repeating timer? Put this in a button script:

Code: Select all

on mouseup
   startTimer
end mouseup

on startTimer
   if the optionKey is down then exit to top
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 1 second
end startTimer
I made the timing 1 second just so you can hear it work. I made the optionKey a way to escape, but this can be any other condition that is tested each iteration.

Because we are using "send in time", this process will run in the "background" as long as the current session is open, and whether or not LC is in front.

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Repeat Timer

Post by jacque » Tue May 22, 2018 6:59 pm

Is this what you want?

Code: Select all

if currentTimerValue = 0 then 
  sendEmail
  send "declare_timer" to me in 0
else 
  send "countDownTimer" && currentTimerValue to me in 1 sec
end if 
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Repeat Timer

Post by ace16vitamine » Tue May 22, 2018 7:47 pm

Ok... Lets explain what I need.

I need to send Emails every X Minutes. To activate this setting I have a Button (Button Start). But sometimes I need to stop this Job, for this I have another Button (Button Stop).

Here is the code for Button Start:

Code: Select all


on mouseUp pButtonNumber
      global stopcountdown
      put 0 into stopcountdown -- This is the that the if Condition is working
 startTimer
 end mouseUp

Code: Select all

on startTimer
   beep 2 --Or sending Email
   send "startTimer" to me in 4 second --Do this every 4 seconds
   if stopcountdown is  1 then exit to top -- AND THIS is not working -> It comes from the other Button
   end startTimer





Here is the code for Button Stop:

Code: Select all

on mouseUp pMouseButton
   
   global stopcountdown
  put 1 into stopcountdown  -- THIS is to Stop the if condition, but it does not work. Why?
   
end mouseUp
So timer is working every 4 Seconds, fine. But I can not stop it with the Stop Button... And the question is... Why?

Stef

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Repeat Timer

Post by FourthWorld » Tue May 22, 2018 8:06 pm

The global is not declared in the startTimer handler.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Repeat Timer

Post by ace16vitamine » Tue May 22, 2018 8:14 pm

Declared, but the same issue. Stop Button is not working

Code: Select all

on startTimer
   global stopcountdown
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 4 second
   
   if stopcountdown is  1 then exit to top
   
end startTimer

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Repeat Timer

Post by Klaus » Tue May 22, 2018 8:28 pm

Hi ace,

the order of commands in not correct, check this:

Code: Select all

on startTimer
   global stopcountdown

   ## !! Here! :-)
   if stopcountdown = 1 then exit to top
   ## !!

   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 4 second     
end startTimer
Best

Klaus

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Repeat Timer

Post by ace16vitamine » Tue May 22, 2018 8:30 pm

aaaah you are right!

Thank you Klaus, it is working now

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Repeat Timer

Post by ace16vitamine » Wed May 23, 2018 4:14 pm

But now it is funny. I am trying so send every 216000 my email. This is working. But I receive this email 5 times, sometimes 1 minute later.

Code: Select all

on startTimer
   global stopcountdown
   
   if stopcountdown = 1 then exit startTimer -- to top

   
   beep 2 --here is where you do your own stuff
   send "startTimer" to me in 216000 seconds
      
  dosendmail
   
end startTimer


But why? It seems that

Code: Select all

on mouseUp pMouseButton
   
   global stopcountdown
   put 1 into stopcountdown
 end mouseUp
does not stop the loop.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Repeat Timer

Post by dunbarx » Wed May 23, 2018 5:50 pm

Hi.

Without seeing the code in handler "doSendMail", it is hard to tell.

One generic solution is to change:

Code: Select all

if stopcountdown <= 1 then exit startTimer -- to top
This often catches situations where the "1" is skipped, ignored or irrelevant. :wink:

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Repeat Timer

Post by jacque » Wed May 23, 2018 8:40 pm

Sometimes the code runs so fast that you will get a backup of pending messages in the queue. To avoid that you should check if the message is already in the queue before sending another one. (This showed up in another thread here recently.)

Code: Select all

on startTimer
   global stopcountdown
   
   if stopcountdown = 1 then exit startTimer -- to top

   
   beep 2 --here is where you do your own stuff
  if "startTimer" is not in the pendingMessages then send "startTimer" to me in 216000 seconds -- CHANGED
      
  dosendmail
   
end startTimer

Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”