JavaScript - 截止日期未重置
JavaScript - deadline not resetting
谁能告诉我为什么截止日期一到(即 'else' 部分)就不会重置?
var timeInMinutes = 1;
var currentTime = Date.parse(new Date());
var deadline = localStorage.deadline;
if(deadline) {
deadline = new Date(deadline);
}else {
deadline = new Date(Date.now() + timeInMinutes*60*1000);
localStorage.deadline = deadline;
}
谢谢。
else
语句只会在第一次时 运行,因为 deadline
之后总是 "truthy"。
由于 else
语句应该 运行 第一次和截止日期过后,请将您的 if
条件更改为:
var deadline = localStorage.deadline;
if(deadline && new Date() < new Date(deadline)) {
deadline = new Date(deadline);
} else {
deadline = new Date(Date.now() + timeInMinutes*60*1000);
localStorage.deadline = deadline;
}
基本上就是说,"If there's a deadline and we haven't hit it, keep using the current deadline. Else, set a new deadline."
谁能告诉我为什么截止日期一到(即 'else' 部分)就不会重置?
var timeInMinutes = 1;
var currentTime = Date.parse(new Date());
var deadline = localStorage.deadline;
if(deadline) {
deadline = new Date(deadline);
}else {
deadline = new Date(Date.now() + timeInMinutes*60*1000);
localStorage.deadline = deadline;
}
谢谢。
else
语句只会在第一次时 运行,因为 deadline
之后总是 "truthy"。
由于 else
语句应该 运行 第一次和截止日期过后,请将您的 if
条件更改为:
var deadline = localStorage.deadline;
if(deadline && new Date() < new Date(deadline)) {
deadline = new Date(deadline);
} else {
deadline = new Date(Date.now() + timeInMinutes*60*1000);
localStorage.deadline = deadline;
}
基本上就是说,"If there's a deadline and we haven't hit it, keep using the current deadline. Else, set a new deadline."