javascript - Switch 语句 (javaScript) 将仅显示 else 条件

标签 javascript switch-statement

我试图在英国的某些时间在网络应用程序中显示某些字符串。这造成了多个问题,首先确保应用程序在英国时间显示这些项目,而不仅仅是用户所在的位置,我想我已经解决了这个问题。

我遇到的问题是我的 switch 只显示 else 语句,并且似乎忽略了 else if。这些案例工作正常。

我确信这很简单,但我看不到。

window.onload = function name() {
  var date = new Date();
  var d = date.getUTCDay();
  var t = new Date()
  var h = t.getUTCHours();
  var adjtime = (((new Date().getTimezoneOffset * -1) / 60) + h);
  var shows = [
    'String 1',
    'String 2',
    'String 3',
    'String 4',
    'String 5',
    'String 6',
    'String 7',
    'String 8',
    'String 9',
  ];
  var currentShow;
  var nextShow;

  switch (d) {
    case 0:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[5];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[5];
        nextShow = shows[6];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[6];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[3];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[1];
      }
      break;
    case 1:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[3];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[7];
      }
      break;
    case 2:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[4];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[1];
      }
      break;
    case 3:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[3];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[1];
      }
      break;
    case 4:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[4];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[7];
      }
      break;
    case 5:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[1];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[1];
        nextShow = shows[2];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[2];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[3];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[3];
      }
      break;
    case 6:
      if (adjtime >= 00 && adjtime <= 02) {
        currentShow = shows[0];
        nextShow = shows[3];
      } else if (adjtime >= 02 && adjtime <= 03) {
        currentShow = shows[3];
        nextShow = shows[4];
      } else if (adjtime >= 03 && adjtime <= 04) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 04 && adjtime <= 10) {
        currentShow = shows[0];
        nextShow = shows[7];
      } else if (adjtime >= 10 && adjtime <= 11) {
        currentShow = shows[7];
        nextShow = shows[8];
      } else if (adjtime >= 11 && adjtime <= 12) {
        currentShow = shows[8];
        nextShow = shows[0];
      } else if (adjtime >= 12 && adjtime <= 17) {
        currentShow = shows[0];
        nextShow = shows[4];
      } else if (adjtime >= 17 && adjtime <= 18) {
        currentShow = shows[4];
        nextShow = shows[0];
      } else if (adjtime >= 18 && adjtime <= 20) {
        currentShow = shows[0];
        nextShow = shows[5];
      } else if (adjtime >= 20 && adjtime <= 21) {
        currentShow = shows[5];
        nextShow = shows[6];
      } else if (adjtime >= 21 && adjtime <= 22) {
        currentShow = shows[6];
        nextShow = shows[0];
      } else {
        currentShow = shows[0];
        nextShow = shows[5];
      }
      break;
  }
  document.getElementById("current-show").innerHTML = currentShow;
  document.getElementById("next-show").innerHTML = nextShow;
}
<p id="current-show"></p>
<p id="next-show"></p>

最佳答案

getTimezoneOffset是方法而不是属性,需要调用getTimezoneOffset()来获取值,

var adjtime = (((new Date().getTimezoneOffset() * -1) / 60) + h);

关于javascript - Switch 语句 (javaScript) 将仅显示 else 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59918360/

相关文章:

enums - 在 Swift 中使用 switch 枚举

JavaScript switch 语句只执行默认情况

javascript - 在将项目提交到购物车时防止页面重新加载。

javascript - Radwindow OnClientClose 在打开时触发

javascript - Lodash/下划线 : Convert array of objects to single object

c - 程序在分成 2 个程序时可以工作,但在同一个程序中使用时则不行

swift - 如何更正此 Swift 枚举?

javascript - 非重复下拉选择

javascript - MVC - 下载文件前后加载屏幕/禁用按钮

swift - 如何将 Swift "switch case is ..." block 转换为面向循环的代码?