A task scheduler for Javascript.
- QPS restriction on different type of task
- Able to rerun a task immediately or on demand after it finishes.
Task defination:
{
name: 'any string',
type: Leona.Task.Types.Simple, // or others in Types
func: function() {
// actual code to run
},
opts: {
delay: 1000, // millisec to be dalayed for tast execution
async: true, // if true, func must accepts a callback parameter for async callback
condition: function() {
return true; // task to be execution until condition is met
},
renew: function() {
return false; // should task to be repeated?
}
}
}Create a task:
var task = Leona.Task.create({
name: 'simple-task',
type: Leona.Task.Types.Simple,
func: function() {
console.log('Hello World');
}
});Execute a task:
task.start();Delay task execution:
var task = Leona.Task.create({
name: 'simple-task',
type: Leona.Task.Types.Simple,
func: function() {
console.log('Hello World');
},
opts: {
delay: 1000 // 1 second
}
});Repeat task execution:
var task = Leona.Task.create({
name: 'simple-task',
type: Leona.Task.Types.Simple,
func: function() {
console.log('Hello World');
},
opts: {
renew: function() {
return true; // repeat task after it finishes.
}
}
});Retry on fail:
Leona.Task.create({
name: 'retry',
type: Leona.Task.Types.Network,
func: function(callback) {
var self = this;
$.ajax({
error: function(xhr, textStatus, errorThrown ) {
self.tryCount++;
},
success: function(data, textStatus, xhr) {
self.retry = 0; // Stop retrying
callback();
}
});
},
opts: {
data: { // Will be passed into func and renew as 'this'.
retry = 3,
tryCount = 0
},
async: true,
renew: function() {
return this.tryCount < this.retry;
}
}
});