Created
June 24, 2013 05:01
-
-
Save IceCreamYou/5847832 to your computer and use it in GitHub Desktop.
Execute a collection of tasks together at a later time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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