gpt4 book ai didi

javascript - 通过 Javascript 添加迄今为止的小时、分钟和秒,然后显示在标题上

转载 作者:行者123 更新时间:2023-12-01 02:20:18 24 4
gpt4 key购买 nike

我有一个简单的脚本,应该向您显示当前日期,并根据该日期留下消息。我试图获取它,以便我可以显示它,但也表明因为下个月即将到来,比如说 25 日及之后,它会说下个月即将到来。我也不确定如何使用 setinterval() 使时间看起来像在计数。

到目前为止,这就是我所拥有的:

var message1 = "Have a nice week!";
var message2 = "The weekend is almost here!";
var message3 = "Enjoy the weekend !";
var dateText = "";


function output() {
var today = new Date();
var dd = today.getDate();
dayValue = today.getDay();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
todaysDate = (mm + "/" + dd + "/" + yyyy);

if (dayValue == 0) {
dateText += "Sunday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message3);
}
if (dayValue == 1) {
dateText += "Monday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message1);
}
if (dayValue == 2) {
dateText += "Tuesday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message1);
}
if (dayValue == 3) {
dateText += "Wednesday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message1);
}
if (dayValue == 4) {
dateText += "Thursday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message2);
}
if (dayValue == 5) {
dateText += "Friday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message2);
}
if (dayValue == 6) {
dateText += "Saturday";
document.write("Today is: " + dateText + " " + todaysDate + " " + message3);
}


}
<body onLoad="output()" ;></body>

谢谢。

最佳答案

您可以使用 setInterval 定期调用输出函数,然后调用该函数 onload。

在随附的代码片段中查找解决方案。

var message1 = "Have a nice week!";
var message2 = "The weekend is almost here!";
var message3 = "Enjoy the weekend !";


function setOutput(){
setInterval(output, 3000);
}
function output() {
var today = new Date();
var dd = today.getDate();
var dayValue = today.getDay();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var hh= today.getHours();
var MM= today.getMinutes();
var ss= today.getSeconds();
var dateText = "";

if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var todaysDate = (mm + "/" + dd + "/" + yyyy + " "+hh+":"+MM+":"+ss);
var titleElement = document.getElementById("title");

if (dayValue == 0) {
dateText += "Sunday";
titleElement.innerHTML = "Today is: " + dateText + " " + todaysDate + " " + message3;
}
if (dayValue == 1) {
dateText += "Monday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message1;
}
if (dayValue == 2) {
dateText += "Tuesday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message1;
}
if (dayValue == 3) {
dateText += "Wednesday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message1;
}
if (dayValue == 4) {
dateText += "Thursday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message2;
}
if (dayValue == 5) {
dateText += "Friday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message2;
}
if (dayValue == 6) {
dateText += "Saturday";
titleElement.innerHTML ="Today is: " + dateText + " " + todaysDate + " " + message3;
}


}
<body onLoad="setOutput()" ;>
<div id="title"></div>
</body>

关于javascript - 通过 Javascript 添加迄今为止的小时、分钟和秒,然后显示在标题上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com