Run a task repeatedly in a fixed duration
A task runner which runs code repeatedly for X seconds and in a given duration of time and calling a step
method with the progress so far.
Most basic example is counting to to a number.
Basic usage
var step = function(t, elapsed){
t = t*t*t;
var value = 300 * t;
if( t > 0.999 )
value = 300;
someElement.innerHTML = value|0;
}
var done = function(){
console.log('done counting!');
}
var settings = {
step : step,
duration : 3,
done : done,
fps : 60
}
var task = new Doin(settings)
task.start()
Run task.pause()
to pause at any moment.
Run task.start()
to continue (un-pause)
Usage for DOM updates
When using Do-in
for DOM updates, like the in the example or for animation purposes, I would recommend not to set an fps
option for the Do-in
instance, but let it run at full-speed and only apply an fps mechanisn within your step
method.
This will ensure smooth consistant frame-rate, and will not drop frames, because Do-in
uses requestAnimationFrame
internally.