Skip to content

Instantly share code, notes, and snippets.

@jonasgeiler
Created July 18, 2021 21:10
Show Gist options
  • Save jonasgeiler/f65a416838046b398fa50b2d6b4b8936 to your computer and use it in GitHub Desktop.
Save jonasgeiler/f65a416838046b398fa50b2d6b4b8936 to your computer and use it in GitHub Desktop.
Super simple job scheduler for TypeScript/JavaScript
export function job(time: number | string, job: Function): number {
let millis: number;
if (typeof time === 'number') {
millis = time;
} else {
if (!isNaN(time)) {
millis = parseInt(time);
} else {
const num = parseInt(time.slice(0, -1));
const unit = time.substr(-1);
if (unit === 's') {
millis = num * 1000;
} else if (unit === 'm') {
millis = num * 60 * 1000;
} else if (unit === 'h') {
millis = num * 60 * 60 * 1000;
} else {
millis = num;
}
}
}
return setInterval(job, millis);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment