Created
January 15, 2016 08:34
-
-
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.
This file contains hidden or 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
(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