Cancel a Timer action on Card Change?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

Cancel a Timer action on Card Change?

Post by bmcgonag » Thu Apr 02, 2020 8:05 pm

I have a timer that re-runs an API call every so many minutes (set by the user), and it runs great as long as I'm on that Card when it runs. But if I've moved to another card, it still tries to run, and fails because it can't find controls referenced in the function I run for the API Call.

I see a few things here and there about cancelling the message called when other actions will create an issue (such as changing cards), but I'm not having success in cancelling the pending message.

Here's what I have (in brief)

Code: Select all

global myPendingMessages

on openCard
   checkOnlineMachines
end openCard

on checkOnlineMachines
   put field APIToken on card "Settings" into apiToken
   if (apiToken = "") or (apiToken is empty) then
      ...show some warning fields to tell the user to get an API Key
   else
      ... do a bunch of stuff that works when on the card.
      else
         answer "Something weird happened here!"
      end if
   end if
   put the label of button "UpdateTimeSel" of card "Settings" into updateTimeFrame 
   send checkOnlineMachines to me in (60 * updateTimeFrame) seconds
   put "updated after" && updateTimeFrame after field "Logging"
end checkOnlineMachines

on closeCard
   repeat for each line thisMessageID in myPendingMessages
      cancel thisMessageID
   end repeat
end closeCard
Any help is very much appreciated. As always.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Cancel a Timer action on Card Change?

Post by Klaus » Thu Apr 02, 2020 8:21 pm

Hi bmcgonag,

please look up terms (like SEND) in the dictionary if they do not work as YOU exspected! :D

You need to fill your myPendingMessages variable at some point or there will be nothing to cancel in your closecard handler!

Code: Select all

global myPendingMessages

...
   put the label of button "UpdateTimeSel" of card "Settings" into updateTimeFrame 
   send "checkOnlineMachines" to me in (60 * updateTimeFrame) seconds

   ## Store the ID of the SEND action!
   put the result into myMessageID
   ###

   ## Now create/manage the LIST of these IDs!
   if myPendingMessages = EMPTY then
     put myMessageID into myPendingMessages
   else
     put CR & myMessageID after myPendingMessages
  end if
  ##
  
   put "updated after" && updateTimeFrame after field "Logging"
end checkOnlineMachines

on closecard
...
Best

Klaus

bmcgonag
Posts: 40
Joined: Thu Mar 13, 2014 6:51 pm

Re: Cancel a Timer action on Card Change?

Post by bmcgonag » Thu Apr 02, 2020 8:53 pm

Thanks klaus. Not that it's not working the way I think it should, just not working the way I understood it from the LiveCode Lessons page I found the information on. Sorry, just trying to work through those lessons, then apply them, but my use cases are just different enough that it throws me off. I appreciate the help.

Post Reply