开发者

Can I use setInterval to regularly rewrite a <span> on a page

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;"开发者_C百科>`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?


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


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


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:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜