Javascript 每当时钟在上午12点或下午12点时刷新网页的程序
我使用javaScript创建了一个基于web的时钟。它以12小时格式输出时间,需要在时钟滴答作响(上午12点或下午12点)时按F5刷新 所以我需要一个程序来刷新网页时自动时钟滴答12上午或下午 我正在检查我的JS时钟,输出是Javascript 每当时钟在上午12点或下午12点时刷新网页的程序,javascript,Javascript,我使用javaScript创建了一个基于web的时钟。它以12小时格式输出时间,需要在时钟滴答作响(上午12点或下午12点)时按F5刷新 所以我需要一个程序来刷新网页时自动时钟滴答12上午或下午 我正在检查我的JS时钟,输出是 Output: 11: 59 : 00 PM Sunday // 23 : 59 : 00 几分钟后我又检查了一遍 Output 12 : 01 : 00 PM Sunday // 0 : 01 : 00 我必须刷新网页才能看到变化 我想要
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>
<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>
函数autoref(){
var now=新日期();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds();
如果(h==0&&s==0){
location.reload();
}否则如果(h==12&&s==0){
location.reload();
}
var trig=设置超时(autoref,500);
}
有什么想法吗
谢谢没有看到你的任何代码,这是我能提供的最好的了
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)
另一种方式
//绘制显示的代码
函数drawDisplay(){
var秒=-1
return()=>{
变量日期=新日期()
如果(date.getSeconds()>seconds | |(date.getSeconds()==0&&seconds==59)){
秒=日期。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>
//变量来检查页面是否已刷新,或者页面是否将每秒刷新一次。
var vrefreshed=localStorage.getItem('refreshed');
如果(vrefreshed的类型===‘未定义’)
vrefreshed=0;
功能时间(){
var d=新日期();
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
如果(vrefreshed==0&&((h==23&&m==59&&s==59)| |(h==11&&m==59&&s==59))){
setItem('refreshed',1);
vrefreshed=1;
$(“#spn”).text('页面已刷新');
location.reload();
}否则{
如果(vrefreshed==1&&((h==23&&m==59&&s==59)| |(h==11&&m==59&&s==59))){
//无所事事
}否则{
localStorage.setItem('refreshed',0);
vrefreshed=0;
$(“#spn”).text('页面将在12或24时刷新');
}
}
}
//每1000毫秒(即每秒)调用time()函数。
设置间隔(时间,1000);
我自己得到了答案很抱歉打扰了你们
这是我的密码
<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>
<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>
你好,德科德
函数刷新(){
var now=新日期();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds();
var out=h+“:”+m+“:”+s;
var trig=设置超时(刷新,500);
document.getElementsByClassName(“dcoder”)[0].innerHTML=out;
如果(h==12&&s==0){
location.reload();
}else如果(h==0&&s==0){
location.reload();
}
}
你的想法和你尝试的代码是什么?我不明白你为什么需要一个程序来刷新页面。您可以在JS中轻松处理此问题。为什么不发布您的代码并解释/扩展您真正想要的内容呢。我想这与am/pm没有更新有关。可能会帮助你查看@MichaelHobbs,还有@MichaelHobbs,我想要你说的。。。我的时钟不仅有AM PM,而且还有Day,每当时钟滴答作响时,12 AM Day的名称应该更改,但我必须刷新页面才能看到更改,所以我需要一个自动程序来完成it@sifr_dot_inJS小时是0-23而不是1-24。您的代码将每250毫秒刷新一次页面,这是错误的。提问者希望它在给定的时间刷新。