我可以使用 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('`<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您忘记在
setInterval
回调中更改/设置now
。You forgot to change/set
now
inside thesetInterval
callback.您有两个语法错误。
document.getElementById('NextorNot').innerHTML = '现在已经太晚了,无法进入');
和document.getElementById('NextorNot').innerHTML = '您仍然可以进入') ;
末尾有一个额外的)
。应该是
You have two syntax errors.
document.getElementById('NextorNot').innerHTML = 'It is too late to enter');
anddocument.getElementById('NextorNot').innerHTML = 'You can still enter');
has an extra)
at the end.Should be
快到了。去掉 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.
您有几个语法错误 - 更新跨度内容时出现右括号。将其更改为:
You have a couple of syntax errors - closing parenthesis when updating the content of the spans. Change it to:
试试这个:jsFiddle。
Try this: jsFiddle.