Created
July 18, 2021 21:10
-
-
Save jonasgeiler/f65a416838046b398fa50b2d6b4b8936 to your computer and use it in GitHub Desktop.
Super simple job scheduler for TypeScript/JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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