Running a function everyday midnight

后端 未结 4 1490
难免孤独
难免孤独 2020-12-05 04:42
walk.on(\'dir\', function (dir, stat) {
    uploadDir.push(dir);
});

I am using Node, and i need make this function run everyday at midnight, this

4条回答
  •  死守一世寂寞
    2020-12-05 05:30

    I use the following code:

    function resetAtMidnight() {
        var now = new Date();
        var night = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate() + 1, // the next day, ...
            0, 0, 0 // ...at 00:00:00 hours
        );
        var msToMidnight = night.getTime() - now.getTime();
    
        setTimeout(function() {
            reset();              //      <-- This is the function being called at midnight.
            resetAtMidnight();    //      Then, reset again next midnight.
        }, msToMidnight);
    }
    

    I think there are legitimate use-cases for running a function at midnight. For example, in my case, I have a number of daily statistics displayed on a website. These statistics need to be reset if the website happens to be open at midnight.

    Also, credits to this answer.

提交回复
热议问题