1. Demand background

Implement a feature that sends emails at a fixed time every day (say 10:00 am every day).

2. Implementation idea

For example, you want to send emails every day at 10:00 a.m.

The basic idea is to write a timer, constantly check whether the time is 10:00, if it is triggered.

Assuming that the number of seconds away from 10:00 is x,

When x is greater than 1 hour, a detection is triggered every 1 hour

When x < 1 hour&&x > 10 minutes, the detection is triggered every 10 minutes

When x < 10 minutes&& x > 1 minute, the detection is triggered every 1 minute

When x is less than 1 minute, the detection is triggered every 1 second

3. The implementation code is as follows

--demo
  -index.js / / call timersService
  -timersService.js // Core code
Copy the code

demo/index.js

const timersService = require('./timersService').timersService;

const sendNotification = async() = > {console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --');
  console.log('Send email notifications! (The logic for sending emails is here!) ');
  console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --');
};


// Email is sent at 10:00 every day
timersService(10.0.0, sendNotification);
Copy the code

demo/timersService.js

const moment = require('moment'); function execWhenTime(hour, minute, second, fn) { const seconds = hour * 3600 + minute * 60 + second; let timer = null; let timerValue = null; let currentH; let currentM; let currentS; let currentSeconds; let active = false; const timerFn = () => { currentH = parseInt(moment().format('HH'), 10); currentM = parseInt(moment().format('mm'), 10); currentS = parseInt(moment().format('ss'), 10); currentSeconds = currentH * 3600 + currentM * 60 + currentS; if (currentSeconds - seconds >= 0 && currentSeconds - seconds <= 3600 && ! active) { clearInterval(timer); fn(); timerValue = 3600001; timer = setInterval(timerFn, timerValue); active = true; } const remainSeconds = seconds - currentSeconds >= 0 ? seconds - currentSeconds : seconds - currentSeconds + 24 * 3600; If (seconds - currentSeconds > 3610 | | seconds - currentSeconds < 0) {the console. The log (` inspection once per hour, and then there were: ${(remainSeconds / 3600).tofixed (2)} hours trigger timing notifications! `); // Execute if (timerValue! == 3600000) { active = false; timerValue = 3600000; clearInterval(timer); timer = setInterval(timerFn, timerValue); }} else {if (seconds-currentSeconds > 610) {console.log(' check every 10 minutes, remaining: ${remainSeconds} seconds trigger timing notification! `); // Run if (timerValue! == 600000) { timerValue = 600000; clearInterval(timer); timer = setInterval(timerFn, timerValue); }} else {if (seconds-currentSeconds > 65) {console.log(' check every 1 minute, remaining: ${remainSeconds} seconds trigger timing notification! `); // Execute if (timerValue! == 60000) { timerValue = 60000; clearInterval(timer); timer = setInterval(timerFn, timerValue); }} else {console.log(' check every second, remaining: ${remainSeconds} seconds trigger timing notification! `); // Execute if (timerValue! == 1000) { timerValue = 1000; clearInterval(timer); timer = setInterval(timerFn, timerValue); }}}}}; timerFn(); } exports.timersService = (hour, minute, second, fn) => { execWhenTime(hour, minute, second, fn); };Copy the code

4.Code

Making code: https://github.com/SimpleCodeCX/myCode/tree/master/execWhenTime