Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Possible to invoke the `animate` and define the on finish callback with one expression in native JavaScript Animation API?

As far as I fond the documentation, it looks like to define the callback for the animation, it's required to:

  1. Instantiate the animation
  2. Define the onfinish callback
  3. Play the animation

Is there a way to do same things with one expression as below?

Snackbar.rootElement.animate([
  {
    opacity: 1,
    transform: "none"
  },
  {
    opacity: 0,
    transform: "translateY(-200%)"
  }
], {
  duration: 500,
  easing: "ease-in"
})
   // ↓ [Warning] It just a desired syntax, NOT A VALID SYNTAX
   .onfinish(() => {
      Snackbar.rootElement.remove();
   });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

onfinish is a setter, not a method - if you assign to it instead of calling it as a function, it'll work:

console.log('start');

const d = document.createElement('div');
const res = d.animate([
  {
    opacity: 1,
    transform: "none"
  },
  {
    opacity: 0,
    transform: "translateY(-200%)"
  }
], {
  duration: 500,
  easing: "ease-in"
})
   .onfinish = (() => {
      console.log('finished');
   });

Another option, with addEventListener:

console.log('start');

const d = document.createElement('div');
const res = d.animate([
  {
    opacity: 1,
    transform: "none"
  },
  {
    opacity: 0,
    transform: "translateY(-200%)"
  }
], {
  duration: 500,
  easing: "ease-in"
})
   .addEventListener('finish', () => {
      console.log('finished');
   });

For similar reasons, the following doesn't work:

const obj = {
  set foo(arg) {
    console.log('called foo');
  }
};

obj.foo(123);

The setter can be invoked by assigning to it, but not by calling it as if it was a function (even though it is a function internally).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!