How do I call a function at a specific time of a day?

Viewed 47

I need to call the studentUpdateAttendanceStart action 10 minutes before a course's endDate. How can I do that?. The code below is what I currently have but the student needs to refresh the page. When I included allowAttendanceUpdateAfter, courseDocId, endDate, id, now, startDate, student, studentTimestamp, and studentUpdateAttendanceStart inside useEffect's dependency array useEffect got called multiple times because I was passing the now variable.

What I'm trying to do: If a course starts at 10:30am and ends at 11:30am, from 11:20am to 11:30am, first check if the student.attendanceStatus is not empty and if the time is between 11:20am to 11:30am. Then check if student has been logged in for at least a certain amount of time to mark them as present else mark the student as absent. What is the best approach to this?. The code that I currently have works but the student needs to refresh the page.

const now = moment().toDate(),
    allowAttendanceUpdateAfter = moment(endDate).subtract(10, 'minutes');

useEffect(() => {
    if (
      now >= allowAttendanceUpdateAfter.toDate() &&
      now <= endDate &&
      !student.attendanceStatus.length
    ) {
      if (allowAttendanceUpdateAfter - startDate < now - studentTimestamp) {
        studentUpdateAttendanceStart(courseDocId, {
          ...student,
          id,
          attendanceStatus: 'Present'
        });
      } else {
        studentUpdateAttendanceStart(courseDocId, {
          ...student,
          id,
          attendanceStatus: 'Absent'
        });
      }
    }
  }, []);
1 Answers

You need a cron job.

Wherever you're hosting the api of this application usually provides such service from the container where you would be able to make time specific periodic requests to the client via http or whatever other method of communication is supported by your host.

Related