0

我遇到了一个 JavaScript 定时输出的问题,它说明了某种类型的问候,然后是时间。代码:

<script language="Javascript">
<!--
{
    var now = new Date();
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds};
    if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    document.write(timemsg)};
// -->
</script>

预期的输出应该是这样的:Wakey Wakey,/早上好/下午/晚上,先生。时间是:(时间)。

4

3 回答 3

2

您的代码中有许多未设置的变量,开头{和结尾}也是语法错误。

下面的代码已更正,只需alert();再次替换document.write();

 var now = new Date();
 var hours = now.getHours();
 var minutes = now.getMinutes();
 var seconds = now.getSeconds();
 if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds}
 alert(timemsg);

此外,您经常这样做,所以我认为值得一提。这个:

The time is: " hours + ':'

需要是

The time is: " + hours + ':'

查看变量的+ 前后。您需要+在字符串中添加变量之前和之后。


边注:

ecmascript 6 的一部分是模板字符串。这是何时使用它们的一个很好的例子。例如:

"Good afternoon, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds

现在可以写成

`Good afternoon, Mr. Person. The time is: ${hours}:${minutes}:${seconds}`

这使它更具可读性。请注意此功能支持的浏览器。

于 2015-09-22T22:17:59.550 回答
0

又过了一会儿,我能够弄清楚:

function startTime() {
    // Clock
    var hello="The time is: ";
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    h = checkTime(h);
    m = checkTime(m);
    s = checkTime(s);

    // Timed Greeting
    if (h == 6 && m == 30 && s == 0){window.alert("Go to school, dude!")};
    if (h < 7){timemsg = "You're up <B>really</B> early, Mr. Lawrence! "};
    if (h > 6 && h <12){timemsg = "Good morning, Mr. Lawrence! "};
    if (h > 11 && h <18){timemsg = "Good afternoon, Mr. Lawrence! "};
    if (h >17){timemsg = "Good evening, Mr. Lawrence! "};
    document.getElementById('txt').innerHTML = timemsg+hello+h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
    if (i<10) {i = "0" + i};  // Add zero in front of numbers < 10
    return i;
}
于 2015-10-01T11:35:50.083 回答
-1

尝试这个:

<script language="Javascript">
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    if (hours < 7){timemsg = "Wakey wakey, Mr. Person. The time is: " + hours + ':' + minutes + ':' + seconds};
    if (hours > 6 && hours <12){timemsg = "Good morning, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours > 11 && hours <18){timemsg = "Good afternoon, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    if (hours >17){timemsg = "Good evening, Mr. Person. The time is: " hours + ':' + minutes + ':' + seconds};
    document.write(timemsg)};
</script>

它应该工作。

于 2015-09-22T22:16:42.590 回答