Question

Given the R3-GUI code below, is there a way to access how much time is left in the timer? The timer ID is returned by set-timer but I am not sure if there is anything I can do with it?

set-timer [print "done"] 60

In other words, what I am looking for in a fake code example:

>> get-timer/time-remaining timer-id
== 0:0:21
Was it helpful?

Solution

The answer can be found by looking at the source of set-timer

>> source set-timer
set-timer: make function! [[
    {Calls a function after a specified amount of time. Returns timer ID reference.}
    code [block!]
    timeout [number! time!]
    /repeat "Periodically repeat the function."
    /local t id
][
    t: now/precise
    if number? timeout [timeout: to time! timeout]
    sort/skip/compare append guie/timers compose/deep/only [(id: guie/timer-id: guie/timer-id + 1) [
        timeout (t + timeout)
        rate (all [
                repeat
                max 0:00 timeout
            ])
        callback (function [] code)
    ]] 2 2
    guie/timeout: true
    id
    ]
]

If the timer is still going, it will be in the guie object.

>> guie/timers
== []

>> set-timer [print "done"] 2
== 5

>> guie/timers
== [5 [
    timeout 11-Aug-2013/22:41:13.381-5:00
    rate none
    callback make function! [[
        /local
    ][print "done"]]
]]

And getting the date value will look like this:

second select guie/timers timer-id
>>b: second select guie/timers 5
==11-Aug-2013/22:41:13.381-5:00
>>c: now/time - b/time
== 0:0:55

If the timer has finished, do-events clears it out. If events are not being run, then the timer will stay even after the time has run out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top