Jump to content

How to stop a timer within it's function


aintaro

Recommended Posts

Hello,

How can I stop a timer within a timer, here is an example to make it clear :

function test() 
local timer = 10 
local theTimer = setTimer(function() 
if (timer > 0) then 
 timer = timer - 1 
else 
--KILLTIMER HERE (already tried killTimer within the function, did not work) 
end 
end, 1000, 0) 
end 

Edited by Guest
Link to comment
  • Moderators
aintaro said:
Cheez3D said:


  
local Timer = nil; 
Timer = setTimer(function() 
    killTimer(Timer); 
end,1000,1); 
  
 

thanks man, this is what I needed

This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them).

This is what you should do (as myonlake sugested):

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 
 
Link to comment
Citizen said:
This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them).

This is what you should do (as myonlake sugested):


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 
 

That is not going to work unless you declare the Timer variable one line before.

  
local Timer = nil; 
Timer = setTimer(function(self) 
    outputChatBox(self); --> userdata; 
end,1000,1,Timer); 
  
 
  
local Timer = setTimer(function(self) 
    outputChatBox(self); --> nil; 
end,1000,1,Timer); 
  
 
Link to comment
  • Moderators

That is not going to work unless you declare the Timer variable one line before.

  
local Timer = nil; 
Timer = setTimer(function(self) 
    outputChatBox(self); --> userdata; 
end,1000,1,Timer); 
  

  
local Timer = setTimer(function(self) 
    outputChatBox(self); --> nil; 
end,1000,1,Timer); 
  

Yup/(correct),

The variable "Timer" received it's value after the setTimer function has returned it's result.

Which means the code inside of the function has been loaded before the returning and the Timer variable doesn't exist till then.

Link to comment
Cheez3D said:
Citizen said:
This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them).

This is what you should do (as myonlake sugested):



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 
 

That is not going to work unless you declare the Timer variable one line before.


  
local Timer = nil; 
Timer = setTimer(function(self) 
    outputChatBox(self); --> userdata; 
end,1000,1,Timer); 
  
 

  
local Timer = setTimer(function(self) 
    outputChatBox(self); --> nil; 
end,1000,1,Timer); 
  
 

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

Link to comment
  • Moderators
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

Hehehe, I don't test my code 90% of the time because my code works at first try 9 times out of 10.

So thanks for the advice but I'll just ignore it. Good luck with your problems in the future.

Link to comment
Why not just do this, instead of all of these complications.
function MyFunction() 
    setTimer(function() 
        -- Your code here 
    end,1000,10) 
end 

let's say you want a certain value to be higher than 0 to keep executing the the timer

something like this :

setTimer(function() 
      if (health > 0 and health < 50) then 
              --execute code here 
      else 
              -- kill timer here 
      end 
end, 1000, 0) 
  
Somethimes you need to kill a timer within it's function as you do not know how many times the timer has to tick 
  
  
 

Link to comment

What are you using the script for even - if you're using it server-side and it's used individually for specific players, you should know that you'd potentially have up to 4096 simulatenous timers running at the same time each second. Can you explain what exactly are you doing that timer for? And could it be moved to client-side, as that would be efficient for the server performance. Besides you can just use getTickCount( ) client-side.

Link to comment
local Timer = nil 
Timer = setTimer(function(myTimer) 
      if (health > 0 and health < 50) then 
              --execute code here 
      else 
              killTimer(myTimer) 
      end 
end, 1000, 0,Timer) 

Try this, but I'm not sure if it'll work, because it doesn't say if setTimer uses live variable, or just what that variable represented for that time.

Link to comment

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 

Link to comment
Citizen said:
aintaro said:
Cheez3D said:



  
local Timer = nil; 
Timer = setTimer(function() 
    killTimer(Timer); 
end,1000,1); 
  
 

thanks man, this is what I needed

This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them).

This is what you should do (as myonlake sugested):


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 
 
local Timer = nil; 
Timer = setTimer(function() 
    killTimer(Timer); 
end,1000,1); 
 

this is not dirty at all,

Whenever the function gets called, local Timer gets a new slot in the memory, so it's never the same timer, even if multiple players execute the function.

So please test code before you comment :)

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...