一个程序,用于在时钟滴答作响时刷新网页 上午 12 点或中午 12 点



我用javaScript创建了一个基于Web的时钟。 它以 12 小时格式输出时间,每当时钟滴答作响 12 AM 或 12 PM 时,需要按 F5 刷新。

所以我需要一个程序来在时钟滴答作响时自动刷新网页 上午 12 点或下午.

我正在检查我的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>

最新更新