Reputation: 321
To call a timer infinite times I am using
function A()
print("Hello")
timer.performWithDelay(100, B, 1)
end
function B()
timer.performWithDelay(500, A, 1)
end
timer.performWithDelay(100, A, 1)
So that If I want to print hello in a particular time interval, I am adjusting it with these two functions. But the problem I am facing is after some time the timer gets slow down and calling function A very rapidly. Can anyone suggest me If I am doing this right? And to resolve the timer issue what should I do?
Thanks in advance.
Upvotes: 2
Views: 2346
Reputation: 7390
If you want to call a timer infinite times, you can use either:
timer.performWithDelay(100, functionName, -1)
or
timer.performWithDelay(100, functionName, 0)
In your case, you need to cancel the timers before calling it again. So, do like follows:
local timer_1,timer_2,timer_3
function A()
print("Hello")
if(timer_1)then timer.cancel(timer_1) end -- cancelling 'timer_1' if exists
if(timer_3)then timer.cancel(timer_3) end -- cancelling 'timer_3' if exists
timer_2 = timer.performWithDelay(100, B, 1)
end
function B()
if(timer_2)then timer.cancel(timer_2) end -- cancelling 'timer_2' if exists
timer_3 = timer.performWithDelay(500, A, 1)
end
timer_1 = timer.performWithDelay(100, A, 1)
Here you can see that, I've created timer objects(timer_1,timer_2 and timer_3), and cancel timers which are possibly in progress, before calling another/same timer.
Keep Coding............. :)
Upvotes: 4