scriptingtclprocxchat2

stop tcl procedure from running more regularly than once every 2 minutes, regardless of how often it is called


I have a piece of code that has a procedure, I only want the proc to trigger, at most every 2 minutes. If it has triggered within the last 2 min, then it should just exit. Currently it has a small delay before it executes (after 5000), but what this seems to do as well, is queue any other execution requests that have occurred in the quiesce time (5 seconds) and then just pummel the queued commands out in a flurry of activity.

Obviously this is missing a significant portion, but
I have considered doing something with variables like:

#gets current time since epoch as var0
set timer0 clock format [clock seconds] -format %s
#gets current time since epoch as var1
set timer1 clock format [clock seconds] -format %s
#calculates elapsed time since epoch in seconds, converts seconds to minutes
set split [ expr (($timer0 - $timer1)/60)/60 ]
if { $split > 2 } {
    my_proc $maybe_1var $maybe_2vars $maybe_3vars  
} else {  
    exit super gracefully  
}  

I can provide snippets of my current code if you like. It is just not nearly as elegant as I imagine it could be, and I am not sure if there are better ways of doing this in Tcl.


Solution

  • One possible solution:

    set lastExecuted ""
    
    proc foo {} {
        global lastExecuted
        set currentTimestamp [clock scan seconds]
        if {$lastExecuted != "" && ($currentTimestamp - $lastExecuted) > 120} {
            exit
        }
        set lastExecuted $currentTimestamp
        # continue execution
    }