Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda