Variable delay in timers with _delay?

I have tried to find a way to dynamically set the delay time for timers. The only solution I have found is using _delay like this:

local myTimer local myDelay = 1000 myTimer = timer.performWithDelay(2000, function() myDelay = myDelay + 100     myTimer.\_delay = myDelay     ... logic here end, -1)

This works but I cannot find _delay in the documentation. Is this some kind of hidden internal feature that “may be subject to change without any previous announcement”?

If you are operating on an object, you can make that more self contained as follows:

local obj = newCircle( 10, 10, 10 ) obj.period = 1000 function obj.timer( self ) if( self.removeSelf == nil ) then return end --Safely exit if removed print(self.period) self.period = self.period - 100 self.period = (self.period \< 100) and 100 or self.period timer.performWithDelay( self.period, self ) end timer.performWithDelay( obj.period, obj) -- delete object in 10 seconds to test safety code timer.peformWithDelay( 10000, function() display.remove( obj ) end )

Oh, and to answer your question, I think the answer is yes.  You should not rely on that.  It is probably pretty stable, but it could change in future releases.

@roaminggamer

I tried your code and it worked like a charm (as always). Thanks!

Note: I just made a small update.  Seems I left out:

  • over time reduction by 100ms
  • safety code for minimum period of 100ms

Anything prefixed with _ indicates that it is private and should never be directly referenced.

If you are operating on an object, you can make that more self contained as follows:

local obj = newCircle( 10, 10, 10 ) obj.period = 1000 function obj.timer( self ) if( self.removeSelf == nil ) then return end --Safely exit if removed print(self.period) self.period = self.period - 100 self.period = (self.period \< 100) and 100 or self.period timer.performWithDelay( self.period, self ) end timer.performWithDelay( obj.period, obj) -- delete object in 10 seconds to test safety code timer.peformWithDelay( 10000, function() display.remove( obj ) end )

Oh, and to answer your question, I think the answer is yes.  You should not rely on that.  It is probably pretty stable, but it could change in future releases.

@roaminggamer

I tried your code and it worked like a charm (as always). Thanks!

Note: I just made a small update.  Seems I left out:

  • over time reduction by 100ms
  • safety code for minimum period of 100ms

Anything prefixed with _ indicates that it is private and should never be directly referenced.