Javascript 我可以使用setInterval定期重写a<;span>;在一页上

Javascript 我可以使用setInterval定期重写a<;span>;在一页上,javascript,innerhtml,setinterval,Javascript,Innerhtml,Setinterval,我不知道任何javascript,但我试图了解以下代码 我得到了下面的代码,可以根据时间向用户发送消息 now = new Date(); if (now.getUTCHours() >= 12) { document.write('`<span id="NextorNot" style="color:red;">`It is too late to enter`</span>`'); } else { document.write('`<sp

我不知道任何javascript,但我试图了解以下代码

我得到了下面的代码,可以根据时间向用户发送消息

now = new Date();
if (now.getUTCHours() >= 12) {
    document.write('`<span id="NextorNot" style="color:red;">`It is too late to enter`</span>`');
}
else {
    document.write('`<span id="NextorNot" style="color:green;">`You can still enter`</span>`');
}
now=新日期();
if(now.getUTCHours()>=12){
document.write('``现在输入'``已经太晚了');
}
否则{
document.write('``您仍然可以输入'`');
}
这很好,但我想让它定期更新,让任何长时间坐在页面上的人都能看到最新信息。经过大量的阅读,我已经做到了这一点,但我看不出我错在哪里

now = new Date();  
if (now.getUTCHours() >= 12)   {  
    document.write('`<span id="NextorNot" style="color:red;">`It is too late to enter`</span>`');  
}  
else {  
    document.write('`<span id="NextorNot" style="color:green;">`You can still enter`</span>`');  
}  
setInterval(
   function() {  
      if (now.getUTCHours() >= 12) {  
        document.getElementById('NextorNot').style.color = 'red';  
        document.getElementById('NextorNot').innerHTML = 'It is too late to enter');  
      }  
      else {  
        document.getElementById('NextorNot').style.color = 'green';  
        document.getElementById('NextorNot').innerHTML = 'You can still enter');  
      }  
   },  
   5000
);  
now=新日期();
如果(now.getUTCHours()>=12){
document.write('``现在输入'``已经太晚了');
}  
否则{
document.write('``您仍然可以输入'`');
}  
设定间隔(
函数(){
如果(now.getUTCHours()>=12){
document.getElementById('NextorNot').style.color='red';
document.getElementById('NextorNot')。innerHTML='It's that late to enter');
}  
否则{
document.getElementById('NextorNot').style.color='green';
document.getElementById('NextorNot')。innerHTML='您仍然可以输入');
}  
},  
5000
);  

有人能帮忙吗?

您忘了在
setInterval
回调中更改/设置
now

您忘了在
setInterval
回调中更改/设置

试试这个:

now=新日期();
if(now.getUTCHours()>=12)
{
document.write(‘现在输入太晚了’);
}
其他的
{
document.write('您仍然可以输入');
}
设定间隔(
函数()
{
现在=新日期();
//警惕(现在);
if(now.getUTCHours()>=12)
{
document.getElementById('NextorNot').style.color='red';
document.getElementById('NextorNot')。innerHTML='现在输入',已经太晚了';
}
其他的
{
document.getElementById('NextorNot').style.color='green';
document.getElementById('NextorNot')。innerHTML='您仍然可以输入';
}
},
5000 );​
试试这个:

now=新日期();
if(now.getUTCHours()>=12)
{
document.write(‘现在输入太晚了’);
}
其他的
{
document.write('您仍然可以输入');
}
设定间隔(
函数()
{
现在=新日期();
//警惕(现在);
if(now.getUTCHours()>=12)
{
document.getElementById('NextorNot').style.color='red';
document.getElementById('NextorNot')。innerHTML='现在输入',已经太晚了';
}
其他的
{
document.getElementById('NextorNot').style.color='green';
document.getElementById('NextorNot')。innerHTML='您仍然可以输入';
}
},
5000 );​

差不多了。去掉if和else分支中“您仍然可以输入”文本后面的右括号“)”。其他一切似乎都起作用了。

差不多了。去掉if和else分支中“您仍然可以输入”文本后面的右括号“)”。其他一切似乎都正常工作。

您有几个语法错误-在更新跨距的内容时用括号括起来。将其更改为:

setInterval(  
function (){  
if (now.getUTCHours() >= 12)  
    {  
        document.getElementById('NextorNot').style.color = 'red';  
    document.getElementById('NextorNot').innerHTML = 'It is too late to enter';  
    }  
    else  
    {  
        document.getElementById('NextorNot').style.color = 'green';  
    document.getElementById('NextorNot').innerHTML = 'You can still enter';  
    }  
},  
5000 );

您有几个语法错误-在更新跨距的内容时关闭括号。将其更改为:

setInterval(  
function (){  
if (now.getUTCHours() >= 12)  
    {  
        document.getElementById('NextorNot').style.color = 'red';  
    document.getElementById('NextorNot').innerHTML = 'It is too late to enter';  
    }  
    else  
    {  
        document.getElementById('NextorNot').style.color = 'green';  
    document.getElementById('NextorNot').innerHTML = 'You can still enter';  
    }  
},  
5000 );

您有两个语法错误

document.getElementById('NextorNot')。innerHTML='It's that late to enter')
document.getElementById('NextorNot')。innerHTML='您仍然可以输入')末尾有一个额外的

应该是

document.getElementById('NextorNot').innerHTML = 'It is too late to enter';
document.getElementById('NextorNot').innerHTML = 'You can still enter';

您有两个语法错误

document.getElementById('NextorNot')。innerHTML='It's that late to enter')
document.getElementById('NextorNot')。innerHTML='您仍然可以输入')末尾有一个额外的

应该是

document.getElementById('NextorNot').innerHTML = 'It is too late to enter';
document.getElementById('NextorNot').innerHTML = 'You can still enter';

不要使用
文档。编写
。它会为没有经验的用户做一些意想不到的事情。不要使用
文档。编写
。它会给没有经验的用户带来意想不到的事情。虽然这在理论上可以回答问题,但在这里包含答案的基本部分,并提供链接供参考。如果我问了一个问题,那么获得完整解决方案的链接对我来说比没有上下文的代码片段更有价值。否则,必须将所有代码解决方案直接带到响应中。imho。虽然这在理论上可以回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。如果我问了一个问题,那么获得完整解决方案的链接对我来说比没有上下文的代码片段更有价值。否则,必须将所有代码解决方案直接带到响应中。依我拙见