Calling timer in a loop

Hello,

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.

Unless the intention is that B does something different than A, having 2 functions seem unnecessary - this would be simpler IMO:

function A() print("Hello") timer.performWithDelay(500, A) end A()

Also, B is not defined when A is defined, so you’re not likely to have both functions get called.

Further, to call a function an infinite number of times using a timer you simply put 0 as the third parameter. You are risking memory problems to do the recursion yourself.

Actually, I want to call firstly function A with delay of time=100 then call the same function with each delay of time=5000. Thats why I was doing this. Can you please suggest me something regarding this?

Hmm, I entirely expected B not to be defined because I believe it is a “forward reference” which should be null. However, this is only true for local variables - which these functions are not.

Anyway, if you want A called after 100 milliseconds and then B called every 5000 milliseconds, you should do this (I have also adjusted the code to be local and to resolve the forward reference):

local function B() print("'ello B") end local function A() print("Hello A") timer.performWithDelay(500, B, 0) end timer.performWithDelay(100, A, 1)

I am moving two objects one after another in this interval.

My first object is in function A and my second object is in function B.

I want both objects to be displayed in this interval of time and generate infinite times. That is why I am calling one function from another.

Can you please help me with this?

If you are trying to use this code to control animations, I think you need a very different approach. Look at transition.* in the documentation: http://docs.coronalabs.com/api/library/transition/index.html

I don’t want to use transition here. My objects are moving with linear velocity in upward direction. I just want to control the timer of each object.

Unless the intention is that B does something different than A, having 2 functions seem unnecessary - this would be simpler IMO:

function A() print("Hello") timer.performWithDelay(500, A) end A()

Also, B is not defined when A is defined, so you’re not likely to have both functions get called.

Further, to call a function an infinite number of times using a timer you simply put 0 as the third parameter. You are risking memory problems to do the recursion yourself.

Actually, I want to call firstly function A with delay of time=100 then call the same function with each delay of time=5000. Thats why I was doing this. Can you please suggest me something regarding this?

Hmm, I entirely expected B not to be defined because I believe it is a “forward reference” which should be null. However, this is only true for local variables - which these functions are not.

Anyway, if you want A called after 100 milliseconds and then B called every 5000 milliseconds, you should do this (I have also adjusted the code to be local and to resolve the forward reference):

local function B() print("'ello B") end local function A() print("Hello A") timer.performWithDelay(500, B, 0) end timer.performWithDelay(100, A, 1)

I am moving two objects one after another in this interval.

My first object is in function A and my second object is in function B.

I want both objects to be displayed in this interval of time and generate infinite times. That is why I am calling one function from another.

Can you please help me with this?

If you are trying to use this code to control animations, I think you need a very different approach. Look at transition.* in the documentation: http://docs.coronalabs.com/api/library/transition/index.html

I don’t want to use transition here. My objects are moving with linear velocity in upward direction. I just want to control the timer of each object.