Skip to content

Instantly share code, notes, and snippets.

@lemiant
Last active December 15, 2015 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemiant/10659622 to your computer and use it in GitHub Desktop.
Save lemiant/10659622 to your computer and use it in GitHub Desktop.
Handling HTML5 audio/video with jQuery promises
// This function plays an audio/video element and creates a promise
// that resolves when the audio/video finishes playing
// It's nice :)
function HTML5Play(target){
var t = $(target);
if(t.length != 1 || !t.is('audio, video')) return undefined; //These are invalid cases
var deferred = new $.Deferred();
t.on('ended', function(){ deferred.resolve(t) })
t[0].play()
return deferred.promise()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment