November 8, 201015 yr Hey, I have a problem, I have my webpage all set out, and I want the time to update every second. I have my function updateTime() in which is displays the time. Then i want this to be updated every second via the setInterval function. So this is what I have: function updateTime() { var now = new Date(); var time = 'The current time is: ' + ( now.getHours() % 12 ) + ':' + pad( now.getMinutes() ) + ':' + pad( now.getSeconds() ) + ' ' + ( now.getHours < 12 ? 'AM' : 'PM' ); document.getElementById("time").innerHTML = time; } Then, my window onload window.onload() { updateTime(); // So the page doesnt have to wait a second for the time to load setInterval( "updateTime()", 1000 ); } Now, the time loads (through the updateTime(); but the time doesnt refresh every second! I really cant work it out, anyone notice anything?
November 8, 201015 yr You're sending the setInterval a string, not a function. To get around that, take out the quotation marks and brackets: window.onload() { updateTime(); // So the page doesnt have to wait a second for the time to load setInterval( updateTime, 1000 ); } That should fix it
Create an account or sign in to comment