Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run Job Periodically

Hi,

Is it possible to execute a function every few seconds without any event ?

Thanks,

Vivek
0 Kudos
8 REPLIES 8
RokuJoel
Binge Watcher

Re: Run Job Periodically

Yes:


sub main()
timer=createobject("rotimespan")
timer.mark()
while true
if timer.totalseconds() > 3 then
result=timedfunction()
print result
end if
?looping"
end while
end sub

function timedfunction() as string
result="function executed"
return result
end function
0 Kudos

Re: Run Job Periodically

Thanks a lot.

Another question: Will anotherFunction and anotherFunction1 get executed ? Or the code will never come out of the while loop ??

sub main()
timer=createobject("rotimespan")
timer.mark()
while true
if timer.totalseconds() > 3 then
result=timedfunction()
print result
end if
?looping"
end while

anotherFunction()
anotherFunction1()
end sub

function timedfunction() as string
result="function executed"
return result
end function
0 Kudos
RokuJoel
Binge Watcher

Re: Run Job Periodically

The code will never exit the loop in the example.

If you want to exit the while loop, you have to either base the loop on a condition:

result="stay in loop"
while=result="stay in loop"
if timer.totalseconds() > 3 then
result=timedfunction()
print result
end if
end while


or you use a condition to exit the loop:

while true
if timer.totalseconds() > 3 then
result=timedfunction()
if result<>"stay in loop" then exit while
end if
end while
0 Kudos

Re: Run Job Periodically

Thanks !

Actually, I want scheduler kind of functionality. I need a function to be executed every few seconds but at the same time rest of the code should be executed too. Like in Java we have quartz scheduler it automatically triggers the action.

The function you created timedfunction() should run let us say every 5 seconds. anotherFunction and anotherFunction1 should be running too.
0 Kudos
RokuJoel
Binge Watcher

Re: Run Job Periodically

Sure, just put the functions you want to execute inside the while loop instead of outside.

- Joel
0 Kudos

Re: Run Job Periodically

But then how will the code which is outside the loop get executed because I do not want to exit the loop.
example : Let us say I divide the screen in two parts left and right. Right part plays the videos and left side pulls the data from the server every 5 seconds and display.

In the following code, it will pull the data from the server but it will not play the video. How can I run the playVideo function ? I can not put the playVideo function call in the while loop.

sub main()
timer=createobject("rotimespan")
timer.mark()
while true
if timer.totalseconds() mod 5 = 0
' call some function to pull the data from the server and display
end if
end while

playVideo()

end sub

function playVideo()
print "playing video"
end function
0 Kudos
RokuJoel
Binge Watcher

Re: Run Job Periodically

you will have to integrate this with your video playback function, for example, set up your roScreen or roImageCanvas, create your event timers, set the video to play in a window, assign the video component's message port to the same port that the screen uses, and initiate playback, then start your while loop. Within the while loop you will listen for messages from the video player and screen, but you won't use the wait function:


video.play()
timer1.mark()
timer2.mark()
timer3.mark()
while true
msg=port.getmessage()
if type(msg)="rovideoPlayerEvent" then
(handle video player events)
else if type(msg)="roImageCanvasEvent" then
(handle image canvas events)
end if
if timer1.totalseconds() > 5 then
function1()
timer1.mark()
end if
if timer2.totalseconds() > 3 then
function2()
timer2.mark()
end if
if timer3.totalseconds() >6 then
function3()
timer3.mark()
end if
end while
0 Kudos

Re: Run Job Periodically

Thanks ! I will try it and will let you know.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.