Last active
December 15, 2015 19:10
-
-
Save lemiant/10659622 to your computer and use it in GitHub Desktop.
Handling HTML5 audio/video with jQuery promises
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
// 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