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

187
Views
¿Hay una mejor manera de cambiar el tiempo de espera en javascript?

En este momento estoy usando esta función para actualizar algunos tiempos de espera sin tener que pasar las devoluciones de llamada.

 private _changeTimeout(timeout: NodeJS.Timeout, at: milliseconds): NodeJS.Timeout { // @ts-expect-error const timeout = setTimeout(timeout._onTimeout, at); clearTimeout(timeout); return timeout; }

¿Hay una mejor manera, sin tener que usar una variable privada que me obligue a usar // @ts-expect-error ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sí: cree su propio contenedor para setTimeout para que no tenga que usar propiedades internas no documentadas de la clase Timeout . El uso de propiedades internas no documentadas siempre será un peligro de mantenimiento.

Algo vagamente como:

 class MyTimeout { private timeout: NodeJS.Timeout | null = null; constructor( private callback: (...args: any[]) => void, public ms: number, paused?: boolean, ) { if (!paused) { this.set(); } } set(ms?: number) { if (typeof ms !== "undefined") { this.ms = ms; } this.timeout = setTimeout((...args) => { this.callback(...args); this.timeout = null; }, this.ms); } clear() { if (this.timeout) { clearTimeout(this.timeout); this.timeout = null; } } change(ms: number) { const running = this.isRunning(); this.clear(); this.ms = ms; if (running) { this.set(ms); } } get isRunning() { return this.timeout !== null; } }

Uso:

 const t = new MyTimeout(() => { // ... }, 1000); // ... t.change(2000);

Enlace del patio de recreo

Eso podría ser un poco exagerado, pero entiendes la idea.

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!