Quiero activar 2 funciones con un tiempo de espera único.
Lo siguiente funciona bien
setTimeout(function() { function1(); // runs first function2(); // runs second }, 1000) Pero en mi caso, tengo un método de clase y una devolución de llamada (función de flecha).
Se ve así (minimizado):
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) } Recibo el siguiente error Expected an assignment or function call and instead saw an expression . ¿Algún truco para arreglar eso?
No está llamando a la función de devolución de llamada, también puede agregar verificación de tipo antes de llamar.
set() { this.active = true; this.timeOut = setTimeout(() => { this.cancel(); typeof this.callback === 'function' && this.callback(); }, 1000) }