每秒更新一次以显示时间
Update every second to display time
我真的很陌生。我希望这个 js 更新每个秒,这样它就像一个时钟或显示时间
也许使用 setInterval?我不知道该怎么做
这是我的代码
忽略我的代码中的印尼语
date = new Date();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
document.write(tanggal+" "+arrbulan[bulan]+" "+tahun+"<br>"+jam+" : "+menit);
是的。 setInterval
就是这样。
您需要将生成日期的代码包装在:
setInterval(function() {
// Load the date here every second
document.getElementById('your-date-element').innerHTML = YOUR_DATE_HERE
}, 1000)
用 your-date-element
代替您希望日期所在的元素的 ID,用 YOUR_DATE_HERE
代替您想要的格式的日期。
此代码将 运行 每 1000 毫秒(1 秒)并相应地更新日期。
嗯,
您可以在您的 html 代码中使用以下代码定位 div:
const myDiv = document.getElementById('myId');
然后使用它来设置它的内部文本,如下所示:
setInterval(() => {
myDiv.innerText = myFunction();
}, 1000);
其中 myFunction 是:
const myFunction = () => {
date = new Date();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
return tanggal + ' ' + arrbulan[bulan] + ' ' + tahun + '<br />' + jam + ' : ' + menit;
}
这应该有效:
<p id="date"></p>
<script>
setInterval(function(){
date = new Date();
seconds = date.getSeconds();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
document.getElementById('date').innerHTML = tanggal+" "+((bulan+1)%12)+" "+tahun+"<br>"+jam+" : "+menit+" : "+seconds;
},1000);
</script>
我真的很陌生。我希望这个 js 更新每个秒,这样它就像一个时钟或显示时间
也许使用 setInterval?我不知道该怎么做
这是我的代码
忽略我的代码中的印尼语
date = new Date();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
document.write(tanggal+" "+arrbulan[bulan]+" "+tahun+"<br>"+jam+" : "+menit);
是的。 setInterval
就是这样。
您需要将生成日期的代码包装在:
setInterval(function() {
// Load the date here every second
document.getElementById('your-date-element').innerHTML = YOUR_DATE_HERE
}, 1000)
用 your-date-element
代替您希望日期所在的元素的 ID,用 YOUR_DATE_HERE
代替您想要的格式的日期。
此代码将 运行 每 1000 毫秒(1 秒)并相应地更新日期。
嗯, 您可以在您的 html 代码中使用以下代码定位 div:
const myDiv = document.getElementById('myId');
然后使用它来设置它的内部文本,如下所示:
setInterval(() => {
myDiv.innerText = myFunction();
}, 1000);
其中 myFunction 是:
const myFunction = () => {
date = new Date();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
return tanggal + ' ' + arrbulan[bulan] + ' ' + tahun + '<br />' + jam + ' : ' + menit;
}
这应该有效:
<p id="date"></p>
<script>
setInterval(function(){
date = new Date();
seconds = date.getSeconds();
menit = date.getMinutes();
jam = date.getHours();
hari = date.getDay();
tanggal = date.getDate();
bulan = date.getMonth();
tahun = date.getFullYear();
document.getElementById('date').innerHTML = tanggal+" "+((bulan+1)%12)+" "+tahun+"<br>"+jam+" : "+menit+" : "+seconds;
},1000);
</script>