gpt4 book ai didi

javascript - 如何使用 javascript 进行日计时器?

转载 作者:行者123 更新时间:2023-12-01 00:09:03 25 4
gpt4 key购买 nike

我需要带日期的计时器。我想每天自动更改 HTML 表格中的行。

例如;如果是 11 日、12 日或 25 日,二月 08 点,则用“Hello!”更改行。 .

function time() {

var xdate = new Date();
var $second = xdate.getSeconds();
var $minutes = xdate.getMinutes();
var $hours = xdate.getHours();
var $day = xdate.getDay();
var $date = xdate.getDate();
var $month = xdate.getMonth();
var $year = xdate.getFullYear();

console.log($date + "/" + $month + "---" + $hours + ":" + $minutes);
setTimeout("time()", 1000);

var x = document.getElementById("help1");

if ($date == (11 || 12 || 25) && $month === 1 && $hours == 08) {
document.getElementById("help1").innerHTML = "Hello"
}
}
time();
<span id="help1">Hi</span>

最佳答案

你的意思可能是

const pad = num => ("0"+num).slice(-2);
function time() {

var xdate = new Date();
var hours = xdate.getHours();
var minutes = xdate.getMinutes();
var seconds = xdate.getSeconds();
var date = xdate.getDate();
var month = xdate.getMonth()+1; // months start at 0
var year = xdate.getFullYear();

x.innerHTML = date + "/" + pad(month) + "---" + pad(hours)+":"+pad(minutes)+":"+pad(seconds);

// day 11 or 12 or 25 and month February and hour 8

if ((date === 11 || date === 12 || date === 25) && month === 2 && hours === 8) {
x.innerHTML = "Hello"
// clearInterval(tId); // this will stop the timer
}
}
var x, tId;
window.addEventListener("load",function() {
x = document.getElementById("help1");
tId = setInterval(time,1000);
});
<span id="help1"></span>

关于javascript - 如何使用 javascript 进行日计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60188914/

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