Skip to content

Instantly share code, notes, and snippets.

@IceCreamYou
Created June 24, 2013 05:01
Show Gist options
  • Save IceCreamYou/5847832 to your computer and use it in GitHub Desktop.
Save IceCreamYou/5847832 to your computer and use it in GitHub Desktop.
Execute a collection of tasks together at a later time.
/**
* Execute a collection of tasks together at a later time.
*
* This is occasionally useful when animating a lot of primitives on a Canvas
* since batching certain operations that change the canvas state can
* significantly improve drawing time (especially in older browsers).
*/
function Batch(before, after) {
var tasks = [];
this.addTask = function(task) {
tasks.push(task);
};
this.clearTasks = function() {
tasks = [];
};
this.execute = function() {
var state = {task: -1, tasks: tasks.length};
before(state);
for (state.task++; state.task < state.tasks; state.task++) {
tasks[state.task](state);
}
after(state);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment