Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script>
// 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.finished === undefined) {
    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;
      }
    });
  }
}
</script>
  
  <div>
    <span>Foo</span>
  </div>
</body>
</html>
 
// Note: The Animation.prototype.finished polyfill is in the HTML pane
let elem = document.querySelector('span');
let animation = elem.animate([
  { transform: 'translateX(0)' }, 
  { transform: 'translateX(10vw)' }
], { 
  duration: 1000,
  fill: 'both'
});
(async () => {
  try { 
    await animation.finished;    
    elem.textContent += ' finished';    
  }
  
  catch (e) {
    elem.textContent += ' aborted';
  }
})();
// Uncomment below to test catch branch:
// setTimeout(_ => {
//   animation.cancel();
// }, 1000);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
simevidaspro
0viewers