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

234
Vistas
callback function (arrow) and class method in TimeOut

I want to trigger 2 functions with a unique timeOut.
The following works fine

    setTimeout(function() {
      function1(); // runs first
      function2(); // runs second
    }, 1000)

But in my case, I have a class method and an callback (arrow function).
It looks like this (minimalized):

    class Timer {
      constructor(callback) {
        this.active = false;
        this.callback = callback;
      }

      cancel() {
        this.active = false;
      }

      set() {
        this.active = true;
        this.timeOut = setTimeout( function() {
          this.cancel();
          this.callback; // HERE is my problem. doesn't run, not casted as a function
        }, 1000)
      }

I get the following error Expected an assignment or function call and instead saw an expression. Any trick to fix that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not calling callback function, you can also add type check before calling.

set() {
  this.active = true;
  this.timeOut = setTimeout(() => {
    this.cancel();
    typeof this.callback === 'function' && this.callback();
  }, 1000)
}
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