timer, wip
This commit is contained in:
32
src/inc/trigger/lib/timer.mjs
Normal file
32
src/inc/trigger/lib/timer.mjs
Normal file
@@ -0,0 +1,32 @@
|
||||
import lt from "long-timeout";
|
||||
|
||||
export default new class timer {
|
||||
constructor() {
|
||||
this.regex = /\d+[smhdwy]|mon/;
|
||||
this.calc = {
|
||||
y: val => val * 365 * 24 * 60 * 60, // years
|
||||
mon: val => val * 30 * 24 * 60 * 60, // months
|
||||
w: val => val * 7 * 24 * 60 * 60, // weeks
|
||||
d: val => val * 24 * 60 * 60, // days
|
||||
h: val => val * 60 * 60, // hours
|
||||
m: val => val * 60, // minutes
|
||||
s: val => val // seconds
|
||||
};
|
||||
}
|
||||
add(time, fn, begin = ~~(Date.now()/1000)) {
|
||||
if(this.regex.test(time)) {
|
||||
let seconds = 0;
|
||||
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
|
||||
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
|
||||
seconds += parseInt(this.calc[mod](val));
|
||||
})
|
||||
const rest = seconds - (begin - ~~(Date.now() / 1000));
|
||||
lt.setTimeout(() => {
|
||||
fn();
|
||||
}, rest * 1000);
|
||||
return `timer eingetragen (${rest}s)`;
|
||||
}
|
||||
else
|
||||
return "nope";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user