github.com/ckxng/wakeup@v0.0.0-20190105202853-90356a5f5a15/Release/assets/js/cron.js (about)

     1  /*  Copyright (C) 2009 Elijah Rutschman
     2  
     3      This program is free software: you can redistribute it and/or modify
     4      it under the terms of the GNU General Public License as published by
     5      the Free Software Foundation, either version 3 of the License, or
     6      any later version.
     7  
     8      This program is distributed in the hope that it will be useful,
     9      but WITHOUT ANY WARRANTY; without even the implied warranty of
    10      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11      GNU General Public License for more details, available at
    12      <http://www.gnu.org/licenses/>.
    13  /*
    14  
    15  /*
    16  a typical cron entry has either wildcards (*) or an integer:
    17  
    18   .---------------- minute (0 - 59) 
    19   |  .------------- hour (0 - 23)
    20   |  |  .---------- day of month (1 - 31)
    21   |  |  |  .------- month (1 - 12)
    22   |  |  |  |  .---- day of week (0 - 6) (Sunday=0)
    23   |  |  |  |  |
    24   *  *  *  *  *
    25  
    26  */
    27  
    28  var Cron = {
    29   "jobs" : [],
    30   "process" : function() {
    31    var now = new Date();
    32    for (var i=0; i<Cron.jobs.length; i++) {
    33     if ( Cron.jobs[i].minute == "*" || parseInt(Cron.jobs[i].minute) == now.getMinutes() )
    34      if ( Cron.jobs[i].hour == "*" || parseInt(Cron.jobs[i].hour) == now.getHours() )
    35       if ( Cron.jobs[i].date == "*" || parseInt(Cron.jobs[i].date) == now.getDate() )
    36        if ( Cron.jobs[i].month == "*" || (parseInt(Cron.jobs[i].month) - 1) == now.getMonth() )
    37         if ( Cron.jobs[i].day == "*" || parseInt(Cron.jobs[i].day) == now.getDay() )
    38          Cron.jobs[i].run();
    39    }
    40    now = null;
    41   },
    42   "id" : 0,
    43   "start" : function() {
    44    Cron.stop();
    45    Cron.id = setInterval("Cron.process()",60000);
    46   },
    47   "stop" : function() {
    48    clearInterval(Cron.id);
    49   },
    50   "Job" : function(cronstring, fun) {
    51    var _Job = {};
    52    var items = cronstring.match(/^([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]+([0-9]+|\*{1})[ \n\t\b]*$/);
    53    _Job.minute = items[1];
    54    _Job.hour = items[2];
    55    _Job.date = items[3];
    56    _Job.month = items[4];
    57    _Job.day = items[5];
    58    _Job.run = fun;
    59    Cron.jobs.push(_Job);
    60    _Job = null;
    61    items = null;
    62   }
    63  }