Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda