Skip to content

Instantly share code, notes, and snippets.

@mig
Created January 12, 2009 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mig/46234 to your computer and use it in GitHub Desktop.
Save mig/46234 to your computer and use it in GitHub Desktop.
// jsCron - by Andrés Nieto
var jsCron = {
items:[],
interval: null,
parse: function(strUnix) {
return strUnix.match(/^(\d+|\*) (\d+|\*) (\d+|\*) (\d+|\*) (\d|\*) +(\w+)/);
},
check: function() {
var hoy = new Date();
var test = [new Date(), hoy.getMinutes(), hoy.getHours(), hoy.getDate(), hoy.getMonth(), hoy.getDay()];
for (var i in this.items) {
var exec = 0;
var t = this.parse(this.items[i][1]);
for (var x in t)
if (t[x] && (t[x] == test[x] || t[x] == "*"))exec++;
if (exec == 5 && this.items[i][0] == 0) {
eval(t[6]).call();
this.items[i][0] = 1;
} else if (exec <5 && this.items[i][0] == 1) {
this.items[i][0] = 0;
}
}
},
set: function(strUnix) {
if (!/^(\d+|\*) (\d+|\*) (\d+|\*) (\d+|\*) (\d|\*) +(\w+)/.test(strUnix)) return new Error("Formato invalido");
this.items.push([0, strUnix]);
},
init: function(seg) {
var seg = seg || 1000;
this.interval = setInterval("jsCron.check()", seg);
}
};
jsCron.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment