Jump to content

How to stop a timer within it's function


aintaro

Recommended Posts

Dude, did you even test your code? it's not working at all. stop posting code that you didn't even bother to test.

Thanks

Did you even test it? I tested it and it works.

Here's my test (ran server-side):

local timer = nil 

timer =

setTimer(function()

    outputDebugString("update!")

    killTimer(timer)

end, 1000, 0)

Closures in lua uses reference to outside variables, and if the scope ends before closure is called, then the variable is saved until the closures have been "free'd" from memory.. I guess that is kinda how closures work in lua.

As a test, I made a script like this:

local timer = nil 

local count = 0

timer =

setTimer(function()

    outputDebugString(tostring(count))

    count = 2

end, 1000, 1)

count = 1

 

setTimer(function()

    outputDebugString("second timer: "..tostring(count))

    count = 3

end, 3000, 1)

The output was:

1 

second timer: 2

I was talking about this code :

function test() 

    local timer = 10

    local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer

        if (timer > 0) then

         timer = timer - 1

        else

            killTimer(selfTimer)

        end

    end, 1000, 0, theTimer) --give theTimer as function parameter

end

try that and tell me it works..

Link to comment

I was talking about this code :

function test() 
    local timer = 10 
    local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer 
        if (timer > 0) then 
         timer = timer - 1 
        else 
            killTimer(selfTimer) 
        end 
    end, 1000, 0, theTimer) --give theTimer as function parameter 
end 

try that and tell me it works..

You replied to the person that wrote the correct code though and said his code didn't work, and you never said any of the code from replies worked, even though the solution was one of the first replies?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...