我可以使用 setInterval 定期重写 吗?在一页上

发布于 2024-10-19 19:40:48 字数 1230 浏览 1 评论 0原文

我不知道任何 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 = 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
);  

有人可以帮忙吗?

I don't know any javascript but was trying to get my head around the following code.

I was given the below code to fire a message to users dependant on time.

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>`');
}

This worked fine but what I wanted to make it update itself at a regular interval for anyone sat on the page for a long period of time would see current information. After lots of reading and I got as far as this but I can't see where I am going wrong.

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
);  

Could anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简美 2024-10-26 19:40:48

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

You forgot to change/set now inside the setInterval callback.

撩人痒 2024-10-26 19:40:48

您有两个语法错误。

document.getElementById('NextorNot').innerHTML = '现在已经太晚了,无法进入');document.getElementById('NextorNot').innerHTML = '您仍然可以进入') ; 末尾有一个额外的 )

应该是

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

You have two syntax errors.

document.getElementById('NextorNot').innerHTML = 'It is too late to enter'); and document.getElementById('NextorNot').innerHTML = 'You can still enter'); has an extra ) at the end.

Should be

document.getElementById('NextorNot').innerHTML = 'It is too late to enter';
document.getElementById('NextorNot').innerHTML = 'You can still enter';
薯片软お妹 2024-10-26 19:40:48

快到了。去掉 if 和 else 分支中“您仍然可以输入”文本后面的右括号“)”。其他一切似乎都有效。

Almost there. Get rid of the closing parenthesis ")" in after the 'You can still enter' text in the if and the else branches. Everything else appears to work.

何处潇湘 2024-10-26 19:40:48

您有几个语法错误 - 更新跨度内容时出现右括号。将其更改为:

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 );

You have a couple of syntax errors - closing parenthesis when updating the content of the spans. Change it to:

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 );
爺獨霸怡葒院 2024-10-26 19:40:48

试试这个:jsFiddle

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 ()
{
    now = new Date();
    //alert( now );
    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 );​

Try this: jsFiddle.

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 ()
{
    now = new Date();
    //alert( now );
    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 );​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文