具有多个变量的 IF 语句
IF Statement with multiple variables
我有一个倒计时应用程序 运行宁多个日期。我想 运行 clearInterval(),当每个到达日期并停止时钟时。
我有我当前的日期和存储的日期:
const currentDate = new Date()
const stageOneA = new Date ('March 8, 2021 00:00:00'),
stageOneB = new Date('March 29,2021 00:00:00'),
stageTwo = new Date('April 12, 2021 00:00:00'),
stageThree = new Date('March 3,2021 00:00:00'),
stageFour = new Date('June 21,2021 00:00:00').getTime()
然后我想写一个IF语句来检查currentDate是否小于或等于每个日期,然后调用clearInterval。这适用于一个变量,但不适用于所有变量。
我认为下面的方法行得通,但行不通。我哪里错了?
if(stageOneA || stageOneB || stageTwo || stageThree || stageFour <= currentDate) {
clearInterval(updateCountdown)
}
这是一个很常见的错误,它是有道理的。
正确的代码可能是最好的解释方式:
if (stageOneA <= currentDate ||
stageOneB <= currentDate ||
stageTwo <= currentDate ||
stageThree <= currentDate ||
stageFour <= currentDate)
{
clearInterval(updateCountdown);
}
您必须单独检查每个阶段。
您的原始代码是说检查前 4 个日期是否存在,然后检查 stageFour
是否小于或等于当前日期。
// Our interval
const interval = setInterval(() => console.log('lalala'), 1000);
// Declare array of times
const times = [
new Date ('March 8, 2021 00:00:00'),
new Date('March 29,2021 00:00:00'),
new Date('April 12, 2021 00:00:00'),
new Date('March 3,2021 00:00:00'),
new Date('June 21,2021 00:00:00'),
];
// Here begins the checking part.
// This would presumably be ran in a loop,
// perhaps in the same interval function?
// Declare the time we are checking against
const othertime = new Date();
// not needed if we want to just use Date.now()
// check against
if (
// use times.reduce to get earliest time
times.reduce(
// times.reduce is passed a function
// that takes a and b as arguments
(a, b) => (
// check which time is smaller
( a.getTime() < a.getTime() )
// and return that
? a : b
)
// compare the smallest time to the time we're checking against
).getTime() < othertime.getTime()
// instead of othertime.getTime(), we can use Date.now()
) {
// do whatever
clearInterval(interval);
}
如果你想让你的 if 语句读起来更好,你可以创建一个小帮手
const stages = [stageOneA, stageOneB, stageTwo, stageThree, stageFour];
const olderThan = date => stage => stage <= date;
if(stages.some(olderThan(currentDate))) {
clearInterval(updateCountdown);
}
我有一个倒计时应用程序 运行宁多个日期。我想 运行 clearInterval(),当每个到达日期并停止时钟时。
我有我当前的日期和存储的日期:
const currentDate = new Date()
const stageOneA = new Date ('March 8, 2021 00:00:00'),
stageOneB = new Date('March 29,2021 00:00:00'),
stageTwo = new Date('April 12, 2021 00:00:00'),
stageThree = new Date('March 3,2021 00:00:00'),
stageFour = new Date('June 21,2021 00:00:00').getTime()
然后我想写一个IF语句来检查currentDate是否小于或等于每个日期,然后调用clearInterval。这适用于一个变量,但不适用于所有变量。
我认为下面的方法行得通,但行不通。我哪里错了?
if(stageOneA || stageOneB || stageTwo || stageThree || stageFour <= currentDate) {
clearInterval(updateCountdown)
}
这是一个很常见的错误,它是有道理的。
正确的代码可能是最好的解释方式:
if (stageOneA <= currentDate ||
stageOneB <= currentDate ||
stageTwo <= currentDate ||
stageThree <= currentDate ||
stageFour <= currentDate)
{
clearInterval(updateCountdown);
}
您必须单独检查每个阶段。
您的原始代码是说检查前 4 个日期是否存在,然后检查 stageFour
是否小于或等于当前日期。
// Our interval
const interval = setInterval(() => console.log('lalala'), 1000);
// Declare array of times
const times = [
new Date ('March 8, 2021 00:00:00'),
new Date('March 29,2021 00:00:00'),
new Date('April 12, 2021 00:00:00'),
new Date('March 3,2021 00:00:00'),
new Date('June 21,2021 00:00:00'),
];
// Here begins the checking part.
// This would presumably be ran in a loop,
// perhaps in the same interval function?
// Declare the time we are checking against
const othertime = new Date();
// not needed if we want to just use Date.now()
// check against
if (
// use times.reduce to get earliest time
times.reduce(
// times.reduce is passed a function
// that takes a and b as arguments
(a, b) => (
// check which time is smaller
( a.getTime() < a.getTime() )
// and return that
? a : b
)
// compare the smallest time to the time we're checking against
).getTime() < othertime.getTime()
// instead of othertime.getTime(), we can use Date.now()
) {
// do whatever
clearInterval(interval);
}
如果你想让你的 if 语句读起来更好,你可以创建一个小帮手
const stages = [stageOneA, stageOneB, stageTwo, stageThree, stageFour];
const olderThan = date => stage => stage <= date;
if(stages.some(olderThan(currentDate))) {
clearInterval(updateCountdown);
}