viveksachdeva
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
10:36 AM
Run Job Periodically
Hi,
Is it possible to execute a function every few seconds without any event ?
Thanks,
Vivek
Is it possible to execute a function every few seconds without any event ?
Thanks,
Vivek
8 REPLIES 8

RokuJoel
Binge Watcher
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
10:48 AM
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
viveksachdeva
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
10:53 AM
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
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

RokuJoel
Binge Watcher
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
04:06 PM
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:
or you use a condition to exit the loop:
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
viveksachdeva
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
04:13 PM
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.
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.

RokuJoel
Binge Watcher
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
04:27 PM
Re: Run Job Periodically
Sure, just put the functions you want to execute inside the while loop instead of outside.
- Joel
- Joel
viveksachdeva
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
04:35 PM
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
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

RokuJoel
Binge Watcher
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2013
05:32 PM
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
viveksachdeva
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2013
02:27 AM
Re: Run Job Periodically
Thanks ! I will try it and will let you know.