Skip to content

Instantly share code, notes, and snippets.

@atleastimtrying
Created January 15, 2016 08:34
Show Gist options
  • Save atleastimtrying/c8c6a087866b525e6e5c to your computer and use it in GitHub Desktop.
Save atleastimtrying/c8c6a087866b525e6e5c to your computer and use it in GitHub Desktop.
A tiny snippet to orchestrate cumulative timeouts in a more intuitive schedule pattern.
(function(){
var run_events = function(schedule, complete){
var running_total_duration = 0;
schedule.forEach(function(thing, index){
setTimeout(function(){
thing.event();
if(index === schedule.length - 1){
complete(running_total_duration, schedule);
}
}, running_total_duration);
running_total_duration += (1000 * thing.duration);
});
};
var events = [
{
duration: 1,
event: function(){
console.log('event 1');
}
},
{
duration: 2,
event: function(){
console.log('event 2');
}
},
{
duration: 1,
event: function(){
console.log('event 3');
}
}
];
run_events(events, function(){
console.log(arguments);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment