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