[英]Page last modified using Javascript with 12 hour clock showing am or pm
[英]A program to refresh web page whenever clock ticks 12 AM or 12 PM
我已经使用javaScript创建了一个基于Web的时钟。 它以12小时格式输出时间,每当时钟在12 AM或12 PM滴答时,都需要按F5刷新。
因此,我需要一个程序,只要时钟在12 AM或PM滴答时自动刷新网页。
我正在检查我的JS时钟,输出是
Output: 11: 59 : 00 PM Sunday // 23 : 59 : 00
几分钟后,我再次检查
Output 12 : 01 : 00 PM Sunday // 0 : 01 : 00
我必须刷新网页才能看到要自动刷新程序的更改
这是我的代码
<body onload="autoref()">
<script>
function autoref(){
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
if(h==0 && s==0){
location.reload();
}else if(h==12 && s==0){
location.reload();
}
var trig = setTimeout(autoref,500);
}
</script>
</body>
有任何想法吗!?
谢谢
没有看到您的任何代码,这是我能提供的最好的。
var amPm = undefined
function drawDisplay () {
// code that draws your display
}
setInterval(()=>{
var hour = (new Date()).getHours()
if (hour === 0 && amPm !== 'am') {
amPm = 'am'
drawDisplay ()
}
if (hour === 12 && amPm !== 'pm') {
amPm = 'pm'
drawDisplay ()
}
},1000)
其他方式
// code that draws your display function drawDisplay() { var seconds = -1 return ()=>{ var date = new Date() if (date.getSeconds() > seconds || (date.getSeconds() === 0 && seconds === 59)) { seconds = date.getSeconds() console.log(date.toLocaleString()) } } } setInterval(drawDisplay(),250)
正如您提到的“无论何时”,我认为它必须每天都在发生,并添加了代码来检查页面是否刷新,以避免连续刷新。
这是代码:
请记住接受答案,如果答案使您满意,请单击“上选”按钮,否则在问题中提供更多信息(例如,您做了什么然后又得到什么),以便此处的成员可以给出正确的答案。
也不要因为经验丰富的评论而灰心。
<span id='spn'>
</span>
<script
src='https://code.jquery.com/jquery-3.3.1.min.js'
integrity='sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8='
crossorigin='anonymous'></script>
<script>
//variable to check whether the page was refreshed or the page will keep on refreshing every second.
var vrefreshed=localStorage.getItem('refreshed');
if (typeof vrefreshed === 'undefined')
vrefreshed = 0;
function time() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
if(vrefreshed==0 && ((h==23 && m==59 && s==59) || (h==11 && m==59 && s==59))){
localStorage.setItem('refreshed', 1);
vrefreshed=1;
$('#spn').text('page is refreshed.');
location.reload();
}else{
if(vrefreshed==1 && ((h==23 && m==59 && s==59) || (h==11 && m==59 && s==59))){
// do nothing
}else{
localStorage.setItem('refreshed', 0);
vrefreshed=0;
$('#spn').text('page will be refreshed at 12 or 24.');
}
}
}
//call time() function every 1000 milliseconds i.e. every second.
setInterval(time, 1000);
</script>
我已经回答了我自己,抱歉打扰你们,这是我的代码
<body onload="refresh()">
<h1>
Hello, Dcoder
</h1>
<div class="dcoder">
</div>
<script>
function refresh(){
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
var out = h+" : "+m+" : "+s;
var trig = setTimeout(refresh,500);
document.getElementsByClassName("dcoder")[0].innerHTML = out;
if(h==12 && s==0){
location.reload();
}else if(h==0 && s==0){
location.reload();
}
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.