-
Notifications
You must be signed in to change notification settings - Fork 4
Scheduled tasks
Sometimes it's needed to execute delayed tasks or tasks that must be executed repeatedly at a defined interval(like cron jobs).
Hot provide a browser like API in order to accomplish such tasks.
In a show you can call the show.setTimeout(..) method in order to execute a delayed task. This method takes two arguments: a function to execute and the number of milliseconds to wait before executing the code.
The method returns a number, representing the ID value of the timer that is set. Use this value with the show.clearTimeout() method to cancel the timer
def delayed():
show.http({'url':'http://example.com'},2000)
show.setTimeout(delayed)This code will make an HTTP request 2 seconds after the setTimeout method has been called.
def id = show.setTimeout ({
show.http [url:'http://example.com']
}, 5000)
show.setTimeout( {
show.clearTimeout(id)
}, 1000)This code will make an HTTP request 5 seconds after the setTimeout method has been called but the delayed task will be canceled one second after the method call.
In order to call a function or evaluates an expression at specified intervals, you can call the show.setInterval(..) method. It takes two arguments: a function to execute and the intervals (in milliseconds) on how often to execute the code.
The show.setInterval(..) method will continue calling the function until show.clearInterval() is called.
The ID value returned by setInterval() is used as the parameter for the clearInterval() method
show.setInterval(function(){ hprint("Hello"); }, 3000);def id = show.setInterval({
show.http [url:'http://example.com']
},1000)
show.setTimeout ({
show.clearTimeout(id)
},10000)This code will launch a task that makes HTTP requests every second during 10 seconds.