EchoFool Posted October 15, 2008 Share Posted October 15, 2008 I have a javascript which creates a live clock which works fine but after a while in FF the browsers hangs and stops responding then I have to close the program with ctrl alt del. When i remove the script it never happens, and it works perfectly fine in IE with the script just not FF. Was wondering if any expert here might know what I did wrong? Hope you can help! function live_clock() { var today = new Date(); var second = today.getSeconds(); var minute = today.getMinutes(); var hour = today.getHours(); var hour24 = today.getHours(); 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(second<10) { second = "0"+second; } if(minute<10) { minute = "0"+minute; } if(hour24<=12) { ampm = "AM"; } else { ampm = "PM" } if(hour24>=12) { hour= hour-12; } if(hour24==0) { hour=12; } document.getElementById('live_clock').innerHTML=(hour+":"+minute+":"+second+" "+ampm+'<br>'+days[day]+', '+date+' '+mns[month]+' '+year); setInterval('live_clock()', 1000); } window.onload = live_clock; Thats the JS script ^ in my html page i have: <div align=center> <div id="live_clock" align="center" class=clock style="background-color: #ffffff;max-width:150px;"> </div> Hope you can help me out! Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 16, 2008 Share Posted October 16, 2008 Well, either IE is dumb, or it is smart. Either way, the problem is that you're using setInterval and you should use setTimeout. setInterval runs that program every x milliseconds, while setTimeout runs it ONCE in x milliseconds. So, by using setTimeout inside your function, you're running the program, then running it every second. But then every second you run it again. So, it's running once, then twice, then four times, then eight times, then sixteen, and so on, all at the same time. That is bombing FF. So, either just change setInterval to setTimeout, OR remove that from your function and change the window.onload to setInterval. Make sense? Quote Link to comment Share on other sites More sharing options...
EchoFool Posted October 17, 2008 Author Share Posted October 17, 2008 Yeah got it working now F1Fan.. thank you 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.