简体   繁体   English

否则,如果陈述无效

[英]Else if statment not working

I've been having a tough time with this code I've been working with. 我一直在努力处理这段代码。

For context, I'm programming a simplistic Tumblr page (not my blog theme, just a page) in which the music, background, and gif will change depending on the time of day. 对于上下文,我正在编程一个简单的Tumblr页面(不是我的博客主题,而是一个页面),其中的音乐,背景和gif会根据一天中的时间而变化。 After much frustration and trial and error, I've gotten most things to work—except one thing. 经过无数次挫折和反复试验后,除了一件事情,我已经完成了大多数工作。

I want to have three times of day (day, sunset, night) however, I can only achieve day and night, for whenever I try to include "else if" it doesn't work. 我想一天有3次(白天,日落,夜晚),但我只能白天和黑夜实现,因为每当尝试包含“ else if”时,它都将失效。 Here's my current code that does work, but only has two times of day out of three (night/sunset): 这里是我的, 的工作电流的代码,但只有一天两次了三个(夜/日落)的:

 var currentTime = new Date().getHours(); if (19 <= currentTime && currentTime < 6) { if (document.body) { document.body.background = "(night bg)"; var url = "(night gif)"; var img = new Image(); img.src = url; document.body.appendChild(img); var source = "(night music)"; var audio = new Audio(); audio.addEventListener("load", function() { audio.play(); }, true); audio.src = source; audio.autoplay = true; audio.loop = true; } } else { if (document.body) { document.body.background = "(sunset bg)"; var url = "(sunset gif)"; var img = new Image(); img.src = url; document.body.appendChild(img); var source = "(day music)"; var audio = new Audio(); audio.addEventListener("load", function() { audio.play(); }, true); audio.src = source; audio.autoplay = true; audio.loop = true; } } 

I've attempted to change 我试图改变

else {
    if (document.body) {
        document.body.background = "(day bg)";

into saying "else if" instead of just "else" on the top, and also adding a time, like so: 在顶部说“ else if”,而不是仅仅“ else”,并添加时间,例如:

 else if (16 <= currentTime && currentTime < 19) {
    if (document.body) {
        document.body.background = "(day bg)";

when that didn't work I've tried multiple things, like doing away with the "if" or making both the "if" and "else" into "else if" but I've gotten nothing to work. 当这种方法不起作用时,我尝试了多种方法,例如取消“ if”或将“ if”和“ else”都设置为“ else if”,但是我什么也没做。 I've also tried to make a third block that was just "else" with the rest of the coding, but that didn't work either. 我还尝试过用剩下的代码制作第三个块,而“其他”块也没用。 Does anybody know a solution and/or what I'm doing wrong? 有人知道解决方案和/或我做错了什么吗?

I think you have to change your code. 我认为您必须更改代码。

if (19 <= currentTime && currentTime < 6) is incorrect. 如果(19 <= currentTime && currentTime <6)不正确。

Because it means 19<= X < 6. It's impossible. 因为这意味着19 <= X <6。这是不可能的。

So you should change if (19 <= currentTime || currentTime < 6) 因此,您应该更改是否(19 <= currentTime || currentTime <6)

 var currentTime = new Date().getHours(); if (19 <= currentTime || currentTime < 6) { if (document.body) { document.body.background = "(night bg)"; var url = "(night gif)"; var img = new Image(); img.src = url; document.body.appendChild(img); var source = "(night music)"; var audio = new Audio(); audio.addEventListener("load", function() { audio.play(); }, true); audio.src = source; audio.autoplay = true; audio.loop = true; } } else if(16 <= currentTime && currentTime < 19){ /* programming Here */ } else { if (document.body) { document.body.background = "(sunset bg)"; var url = "(sunset gif)"; var img = new Image(); img.src = url; document.body.appendChild(img); var source = "(day music)"; var audio = new Audio(); audio.addEventListener("load", function() { audio.play(); }, true); audio.src = source; audio.autoplay = true; audio.loop = true; } } 

I think your solution can be improved. 我认为您的解决方案可以改善。 If you look at your code, you see that there are a lot of repetition for each event. 查看代码,您会发现每个事件都有很多重复。

If you want to add another event, there is even more code to add. 如果要添加另一个事件,则需要添加更多代码。

I suggest that you make a schedule in an array instead. 我建议您改为在阵列中制定计划。 That makes it much more easy to add new events. 这使得添加新事件变得更加容易。

// Define the schedule. start= start hour, end = end hour.
// With start=6 and end=19, the schedule will be used between
// 06:00 - 18:59.
// The schedules will be searched in order, and the first matching
// schedule will be used. Therefore the default schedule (night) must
// be last in the list.
var schedule = [
  { start : 6, end:19, bbg:"(sunset bg)", img:"(sunset gif)", music:"(sunset music)" },

  // Default schedule, must be last in list.
  { start : 0, end:24, bbg:"(night bg)", img:"(night gif)", music:"(night music)" }
];

var currentHour = new Date().getHours();
var currentSchedue;

// Loop though schedules    
for (var idx=0; idx < schedule.length; idx+=1) {
  currentSchedule = schedule[idx];

  // Find first matching schedule
  if (currentSchedule.start <= currentHour && currentSchedule.end > currentHour) {
    document.body.background =  currentSchedule.bbg;

    var img = new Image();
    img.src = currentSchedule.img;
    document.body.appendChild(img);

    var audio = new Auido();
    audio.src = currentSchedule.music;
    audio.autoplay = true;
    audio.loop = true;

    break; // stop loop if matching schedule was found
  }
}

If you still want to use if-statements, here is a working example: 如果您仍想使用if语句,请参见以下示例:

if ( 6 <= currentHour && currentHour < 16 ) {
  // Daytime, valid from 06:00 - 15:59
  console.log('Day', currentHour);
}
else if ( 16 <= currentHour && currentHour < 19 ) {
  // Evening  valid from 16:00 - 18:59
  console.log('Evening', currentHour);
}
else {
  // Night, valid 00:00 - 05:59 and 19:00 - 24:00
  console.log('Night', currentHour);
}

Logic error in your if-statement : 您的if陈述式中的逻辑错误

if (19 <= currentTime && currentTime < 6) {
}
  • 19 <= currentTime is only true if currentTime is 19 or higher 19 <= currentTime仅在currentTime为19或更高时才为true
  • currentTime < 6 is only true if currentTime is less than 6. 仅当currentTime小于6时currentTime < 6才是true。

They can't be true at the same time. 他们不能同时是真实的。

var currentTime = new Date().getHours();
if (currentTime > 19 && currentTime < 6) {
  // Night
} else if (currentTime > 6 && currentTime < 17) {
  // Day
} else {
  // Sunset
}

For details see here 详情请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM