Вместо тестирования через интервал я бы использовал метод setTimeout
для установки будильника.
Нет смысла тратить время на вычисления, когда мы знаем, когда это будет сделано.
// ES6 CLASS
class Alert {
constructor(timestamp) {
this.timestamp = timestamp;
this._passed = false;
this.callbacks = [];
// Test as soon as possible
setTimeout(this.test.bind(this), 0);
}
get passed() {
return this._passed;
}
test() {
if (this.timestamp.getTime() <= Date.now()) {
//Test again after time difference mark as passed
this._passed = true;
//Fire all callbacks
this.callbacks.forEach(cb => cb());
} else {
//Test again after time difference
setTimeout(this.test.bind(this), this.timestamp.getTime() - Date.now());
}
return this;
}
then(callback) {
if (this._passed) {
callback();
} else {
this.callbacks.push(callback);
}
return this;
}
}
//TEST
// Fire in 10 seconds
new Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
// Fire in 4 seconds
new Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
new Alert(threeOClock)
.then(a => console.log("It's 15 or more!"));
p {
margin: 200px 0px;
}
// TYPESCRIPT CLASS
class Alert {
constructor(timestamp) {
this.timestamp = timestamp;
this._passed = false;
this.callbacks = [];
// Test as soon as possible
setTimeout(this.test.bind(this), 0);
}
get passed() {
return this._passed;
}
test() {
if (this.timestamp.getTime() <= Date.now()) {
//Test again after time difference mark as passed
this._passed = true;
//Fire all callbacks
this.callbacks.forEach(cb => cb());
}
else {
//Test again after time difference
setTimeout(this.test.bind(this), this.timestamp.getTime() - Date.now());
}
return this;
}
then(callback) {
if (this._passed) {
callback();
}
else {
this.callbacks.push(callback);
}
return this;
}
}
//TEST
// Fire in 10 seconds
new Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
// Fire in 4 seconds
var fourSecondAlert = new Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
РЕДАКТИРОВАТЬ 1 - Обещания
Если вы довольны обещаниями и просто нуждаетесь в простой тревоге, то это должно сделать это:
function Alert(date) {
return new Promise(function(res) {
if (Date.now() >= date.getTime()) {
res();
} else {
setTimeout(res, date.getTime() - Date.now());
}
});
}
//TEST
Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
Alert(threeOClock)
.then(a => console.log("It's 15 or more!"));
Создать объект Date и установить время
Самый простой способ создать метку времени - создать Date
объект и изменить его, используя setHours :
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
console.log(threeOClock.toTimeString());