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

242
Views
javascript: cómo llamar a un código que se llamará solo una vez para un evento

Quiero ejecutar un código que se llamará solo una vez para un evento. Entonces, ¿cuáles serán las opciones correctas?

Opción 1:

 var EventSource = require("eventsource"); const source = new EventSource(); let callindex = 0; source.once('event', () =>{ console.log("called"); }); source.emit('event');

Opcion 2:

 var EventSource = require("eventsource"); const source = new EventSource(); let callindex = 0; source.on('event', () =>{ if(callindex == 0){ console.log("called"); } }); source.emit('event');

Opción 3:

 var EventSource = require("eventsource"); const source = new EventSource(); let callindex = 0; source.on('event', () =>{ if(callindex == 0){ console.log("called"); } callindex++; }); source.emit('event');

Opción 4

 var EventSource = require("eventsource"); const source = new EventSource(); let callindex = 0; source.on('event', () =>{ console.log("called"); source.removeAllListeners('event'); }); source.emit('event');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Por qué necesita usar algo como EventSource ? ¿No sería suficiente el Nodo estándar?

 const handleCustomApplicationEvent = () => { console.log('hello'); window.removeEventListener('application:event', handleCustomApplicationEvent, true); } window.addEventListener('application:event', handleCustomApplicationEvent, true); /** trigger it */ const event = new CustomEvent('application:event'); window.dispatchEvent(event); const event2 = new CustomEvent('application:event'); window.dispatchEvent(event2);

Tenga en cuenta que event2 no será capturado

---------------------- Editar ----------------------

La respuesta de @callmenikk es aún mejor.

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!