拔出我的头发试图解决这个问题。我想通过innerHTML根据星期几显示一条消息。我在这里和其他地方看过无数类似的问题,但似乎没有任何工作。我已经尝试过if语句的各种更改,但没有任何成功。我尝试的所有内容都会返回“未定义日期”错误。
提前致谢。
(另外,可能相关:我是Javascript的新手)
JS:
print('Mine: ', timeit.timeit(stmt=stAditya, number=1000000)) # 5.524377981713729
print('Jatimir:', timeit.timeit(stmt=stTwo, number=1000000)) # 6.971933613624651
print('Kasramvd:', timeit.timeit(stmt=oth, number=1000000)) # 15.090121147017014
答案 0 :(得分:0)
day
是startTime
函数的局部变量。它在该功能之外不可用,并且您尝试在if (day == "Fri")
中访问它。在startTime()
内移动if-else语句,它应该有效。
答案 1 :(得分:0)
问题在于day
变量。它是在startTime
函数内声明的,因此无法在if
语句
var day = '';
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date();
var weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tues";
weekday[3] = "Weds";
weekday[4] = "Thurs";
weekday[5] = "Fri";
weekday[6] = "Sat";
day = weekday[today.getDay()];
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var month = new Array(12);
month[0] = "Jan";
month[1] = "Feb";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "Aug";
month[8] = "Sept";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
var currentMonth = month[today.getMonth()];
var year = today.getFullYear();
// add a zero in front of numbers<10
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
document.getElementById('currentTime').innerHTML = day + " " + h + ":" + m + ":" + s;
t = setTimeout(function() {
startTime()
}, 500);
}
startTime();
if (day == "Fri") {
document.getElementById("timeText").innerHTML = "Nearly the weekend!";
} else if (day == "Sat") {
document.getElementById("timeText").innerHTML = "It's Beer O'Clock";
} else {
document.getElementById("timeText").innerHTML = "JS Failed";
}
&#13;
<div id="currentTime"></div>
<div id="timeText"></div>
&#13;
答案 2 :(得分:0)
为我做了一些调整
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date();
var weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tues";
weekday[3] = "Weds";
weekday[4] = "Thurs";
weekday[5] = "Fri";
weekday[6] = "Sat";
var day = weekday[today.getDay()];
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var month = new Array(12);
month[0] = "Jan";
month[1] = "Feb";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "Aug";
month[8] = "Sept";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
var currentMonth = month[today.getMonth()];
var year = today.getFullYear();
// add a zero in front of numbers<10
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
document.getElementById('currentTime').innerHTML = day + " " + h + ":" + m + ":" + s;
if (day == "Fri") {
document.getElementById("timeText").innerHTML = "Nearly the weekend!";
} else if (day == "Sat") {
document.getElementById("timeText").innerHTML = "It's Beer O'Clock";
} else {
document.getElementById("timeText").innerHTML = "JS Failed";
}
}
startTime();
&#13;
<div id="currentTime"></div>
<div id="timeText"></div>
&#13;