Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Call function after some time — Gideros Forum

Call function after some time

rrraptorrrraptor Member
edited November 2015 in General questions
I need to call some of the functions after some time. I've done this like that:
function doAfterDelay(delay, f)
	local t = Timer.new(delay*1000, 1)
	t:start()
	t:addEventListener(Event.TIMER_COMPLETE, f, t)
end
and call:
if *something* then
	doAfterDelay(0.08, function(t, event)
		-- some code here
		t = nil
		event:stopPropagation()
	end)
end
Is that method good? Can it cause a memory leak, or I do not need to worry about it?
I have another solution, but this is a bit easier to understand)))

P.S. Sorry if my eng. is not good enough :)

Comments

  • piepie Member
    Accepted Answer
    You're doing the hard way what Timer.delayedCall(ms, function) does :)
    Timer.delayedCall(1000, function() print("this is delayed by 1 second: 1000 ms") end)
    I don't know why people are afraid of timers though :)
    One alternative could be to keep a counter in enterframe, and do something on certain count.

    Likes: rrraptor

    +1 -1 (+1 / -0 )Share on Facebook
  • You're doing the hard way what Timer.delayedCall(ms, function) does :)
    Wow, didnt know about that, thanx :)
    One alternative could be to keep a counter in enterframe, and do something on certain count.
    I dont need EnterFrame event. May be later, but...i dont think so.

  • Even better, that function returns Timer object, so you can use it later on, like stopping, it, etc.
    local timer = Timer.delayedCall(100, function() end)
    timer:stop()

    Likes: pie, antix, Tom2012

    +1 -1 (+3 / -0 )Share on Facebook
  • @ar2rsawseen hi :) Whats the advantage of using that code? Will a delayed call timer not be garbage collected once it is finished?

    Asking because my game has a zillion delayedcall timers :)
  • If you won't hold the reference to it, then it will be garbage collected.
    Underneath Timer.delayedCall does exactly as creating new timer, setting events, starting timer and returning it.

    So it would behave the same way, only less code for you to write
Sign In or Register to comment.