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

168
Vistas
Best way to bind a variable to object;

I have an array of objects. For each object I need to set a schedule.

'node-schedule'

to reset the schedule I need it in a variable.

function setSchedule(ticket) {
  const date = new Date(2012, 11, 21, 5, 30, 0);

  const job = schedule.scheduleJob(date, function () {
    console.log('The world is going to end today.');
  });
}

how can I bind the object to his own schedule function? I mean, later I need to know which schedule I need to reset.

I would to it something like, but I think there is a better way?

const job[ticket.id] = schedule.scheduleJob(date, function () {
  console.log('The world is going to end today.');
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The job variable will be available in the closure of the scheduled callback function, if referenced.

Consider, for instance, the following code, which is very similar to your own situation:

const handle =
  setInterval(function() {
    console.log("callback");
    clearInterval(handle);
  }, 100)

The callback fired once, then was cancelled by means of the handle variable that exists in an outer scope.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Ok, now i solve it this way

function setSchedule(ticket) {
  if (ticket.blocked) {
    const date = new Date(ticket.blockedDate);
    scheduleJob[ticket._id] = schedule.scheduleJob(
      {
        date: date.getDate(),
        month: date.getMonth(),
        year: date.getFullYear(),
      },
      () => {
        console.log('Run' + ticket._id);
        scheduleJob[ticket._id].cancel();
        Ticket.updateOne(
          { _id: ticket._id },
          { blocked: false, blockedDate: null }
        )
          .then(() => {})
          .catch((error) => {
            console.log(error);
          });
      }
    );
  }
  if (!ticket.blocked && scheduleJob[ticket._id]) {
    scheduleJob[ticket._id].cancel();
  }
}

What do you think? Is these a common way to use the id for array?

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