Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
vbacct
Level 7

Is there a way to execute a function every few secs

I am looking for a function that does something similar to JavaScript's SetInterval() method, basically execute a function every few seconds/millisecs. And keep executing the function until the time interval is cleared. Looks like the roTimeSpan can be used to execute a function after certain time interval but how to make it run continuously until the time interval is cleared?
0 Kudos
3 REPLIES 3\
RokuJoel
Roku Employee
Roku Employee

Re: Is there a way to execute a function every few secs

roTimeSpan should work for what you are trying to do, you'll likely need several instances and use it in several ways, here's one way:

Example: exec a function for a specific duration:


timer=createobject("roTimeSpan")
timer.mark()
maxExecutionTime=50
while timer.totalseconds() < maxExecutionTime
myfunction()
end while


0 Kudos
RokuMarkn
Level 8

Re: Is there a way to execute a function every few secs

If you're looking for a way to essentially run a function "in the background" while other things are happening, you'll need to integrate it into whatever event loop is running at the time. Just make sure your wait has a timeout, and check your timer to see if it's time to run the function. Something like:


next_call_time = 0
while true
wait(100, msgport)
t = timer.TotalSeconds()
if t > next_call_time then
next_call_time = t + interval
call_my_function()
end if
... rest of event loop ....


--Mark
0 Kudos
vbacct
Level 7

Re: Is there a way to execute a function every few secs

Thanks RokuJoel and RokuMarkn.
0 Kudos