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

167
Views
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 answers
Answer question

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 Report

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