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

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

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 Relatório

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 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