Skip to content

Instantly share code, notes, and snippets.

@porsager
Forked from simevidas/finished-polyfill.js
Last active May 17, 2020 21:26
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 porsager/44f940295739eeac46e6a87e90248224 to your computer and use it in GitHub Desktop.
Save porsager/44f940295739eeac46e6a87e90248224 to your computer and use it in GitHub Desktop.
Animation.prototype.finished polyfill
// only polyfill .finished in browsers that already support animate()
if (document.body.animate) {
// Chrome does not seem to expose the Animation constructor globally
if (typeof Animation === 'undefined') {
window.Animation = document.body.animate({}).constructor;
}
if (!Animation.prototype.hasOwnProperty('finished')) {
Object.defineProperty(Animation.prototype, 'finished', {
get() {
if (!this._finished) {
this._finished = this.playState === 'finished' ?
Promise.resolve() :
new Promise((resolve, reject) => {
this.addEventListener('finish', resolve, {once: true});
this.addEventListener('cancel', reject, {once: true});
});
}
return this._finished;
}
});
}
}
@porsager
Copy link
Author

Fix support for Safari 13.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment