EchoFool Posted October 22, 2008 Share Posted October 22, 2008 I have a JS time which is going wrong when the seconds reach 59 to 00. When it hits 00 it then continues to count like this: 00 09 01 00 And only till i refresh the page will it kick back into what ever the server time is.... why is it happening? My script: <div id="live_clock" align="center" class=PositiveMoney style="background-color: #b4cbe8;max-width:150px;"> </div> <script language='javascript'> <?php echo("var second = ".date('s').";\n"); echo("var minute = ".date('i').";\n"); echo("var hour = ".date('g').";\n"); echo("var hour24 = ".date('G').";\n"); ?> function live_clock() { var today = new Date(); second = parseInt(second + 1); if(second < 10){ second = "0"+second; } if (second == 60){ minute = parseInt(minute + 1); second = 00; } if(minute < 10){ minute = "0"+minute; } if (minute == 60){ minute = 00; second = 00; hour = parseInt(hour + 1); } if(hour < 10){ hour = "0"+hour; } var ampm = ""; var day = today.getDay(); var date = today.getDate(); var month = today.getMonth(); var year = today.getFullYear(); var days = new Array(); days[0] = "Sunday"; days[1] = "Monday"; days[2] = "Tuesday"; days[3] = "Wednesday"; days[4] = "Thursday"; days[5] = "Friday"; days[6] = "Saturday"; var mns = new Array(); mns[0] = "January"; mns[1] = "February"; mns[2] = "March"; mns[3] = "April"; mns[4] = "May"; mns[5] = "June"; mns[6] = "July"; mns[7] = "August"; mns[8] = "September"; mns[9] = "October"; mns[10] = "November"; mns[11] = "December"; if(hour24<=12) { ampm = "AM"; } else { ampm = "PM" } document.getElementById('live_clock').innerHTML=(hour+":"+minute+":"+second+" "+ampm+'<br>'+days[day]+', '+date+' '+mns[month]+' '+year); } setInterval('live_clock()', 1000); window.onload = live_clock; </script> hope you can see my mistake. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 23, 2008 Share Posted October 23, 2008 I'm not going to fix your particular problem because I think your code is way overblown. instead I'll post cleaner code that will accomplish what you want. The process you are taking also makes no sense. You are artificially creating the current time from a server-side script and then trying to keep that time current. Why not just use JavaScript to keep the current time? If you need to show the server time, then just echo the server time to the page and use that to determine the correct offset - and still use JS to keep the time. <html> <head> <script type="text/javascript"> var daysAry = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var mnthAry = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; function padVal(numInt) { var padVal = '0' + numInt.toString(); return padVal.substr(padVal.length-2, 2); } function live_clock() { var today = new Date(); var hours = (today.getHours()<=12) ? padVal(today.getHours()) : padVal(today.getHours()-12); var ampm = (today.getHours()<=12) ? 'AM' : 'PM'; var htmlOutput = ''; htmlOutput += hours + ':' + padVal(today.getMinutes()) + ':' + padVal(today.getSeconds()); htmlOutput += ' ' + ampm + '<br>' + daysAry[today.getDay()] + ', '; htmlOutput += today.getDate() + ' ' + mnthAry[today.getMonth()] + ' ' + today.getFullYear(); document.getElementById('live_clock').innerHTML = htmlOutput; return; } setInterval('live_clock()', 1000); window.onload = live_clock; </script> </head> <body> <div id="live_clock" align="center" class="PositiveMoney" style="background-color:#b4cbe8;max-width:150px;"> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
EchoFool Posted October 23, 2008 Author Share Posted October 23, 2008 If you need to show the server time, then just echo the server time to the page and use that to determine the correct offset - and still use JS to keep the time This is what i been trying to do for ages, show server time but i've got no idea precisely how its done. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.