Timer function

Hi All,
For my idea I need to call my function 10…100 times per second to make some calculation.
To do this I’m tried to use imp.wakeup and I got stack overflow error: ERROR: stack overflow, cannot resize stack while in a metamethod

Here the code:
`function timer() {
imp.wakeup(0.1, timer());
}

timer();`

Do we have any limitation of imp.wakeup usage?
Could someone point me to what I’m doing wrong?

Do we have any other ways to call function periodically?

The problem here is that you need to remove the brackets in the imp wakeup, like this:

imp.wakeup(0.1, timer);

…otherwise, it’s calling the timer function when evaluating the expression from within timer and recursing until it runs out of memory.

Thanks Hugo! This helps.

I’m trying to add parameter to timer function and got the same error:

function timer(cycles) { cycles--; if (cycles) imp.wakeup(0.01, timer(cycles)); server.log("timer"); } timer(100);

So, as I understand, there no way to use function with parameters as callback for imp.wakeup.

The function called at wake-up time can’t have parameters, no. But that function can call other functions, which do:
function timer(cycles) { cycles--; if (cycles) imp.wakeup(1, function() { timer(cycles) } ); server.log("timer "+cycles); } timer(10);

Peter

This hack works.
Thanks.

This would be a good one for the WIKI