Skip to content

Timer, pass variable #727

Answered by dnaldoog
Tonfisch asked this question in Q&A
Jul 13, 2025 · 4 comments · 13 replies
Discussion options

You must be logged in to vote

I think you have two choices:

  1. closures
  2. metatables
local counter = (function()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end)()

myMethod = function(--[[ CtrlrComponent --]] comp --[[ MouseEvent --]], event)
-- rename the TimerCallback to either TimerCallback2 or TimerCallback
    timer:setCallback(1, TimerCallback2) -- TimerId 1
    timer:startTimer(1, 200)
end

TimerCallback = function(timerId)
    if timerId == 1 then
        local count = counter()  -- Call the counter function to get and increment
        panel:getLabelComponent("out"):setText(String(count))
        
        if count >= 10 then
            panel:getLabelComponent("out"):append(…

Replies: 4 comments 13 replies

Comment options

You must be logged in to vote
1 reply
@Tonfisch
Comment options

Answer selected by Tonfisch
Comment options

You must be logged in to vote
10 replies
@dnaldoog
Comment options

@Tonfisch
Comment options

@Tonfisch
Comment options

@dnaldoog
Comment options

@Tonfisch
Comment options

Comment options

You must be logged in to vote
2 replies
@Tonfisch
Comment options

@dnaldoog
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants