rmw_crontab

Given a time interval, the task is run periodically, and the default interval has 1 minute,1 hour or 1 day

export in your deps.js

export {mcron,hcron,dcron} from 'https://deno.land/x/rmw_crontab@0.0.2/lib/index.js'

source code

import {_try} from './deps.js'

export class Cron
  constructor:(@interval, @delay)->
    @job = []

  run:->
    {interval} = @
    for i from @job
      i[0] -= interval
      if i[0] <= 0
        i[0]=i[1]
        _try i[2]
    return


  add:->
    @timer = setInterval(
      @run.bind(@)
      @interval*1000
    )

    add = (interval, job, delay)->
      if delay == undefined
        {delay} = @
      interval = interval * @interval
      setTimeout(
        =>
          _try job
        delay*1000
      )
      @job.push [interval+delay, interval, job]

    @add = add.bind(@)
    add.apply @, arguments

MCron = new Cron(60, 3)
HCron = new Cron(3600, 30)
DCron = new Cron(3600*24, 60)

export mcron = MCron.add.bind(MCron)
export hcron = HCron.add.bind(HCron)
export dcron = DCron.add.bind(DCron)


use example

coffeescript version

import {mcron} from './index.js'


do =>
  mcron 1,=>
    console.log new Date().toLocaleString()

javascript version

// Generated by CoffeeScript 2.5.1
import {
  mcron
} from './index.js';

(() => {
  return mcron(1, () => {
    return console.log(new Date().toLocaleString());
  });
})();

//# sourceMappingURL=index_test.js.map