rugzo Posted August 7, 2009 Share Posted August 7, 2009 Hi All, i have a code which shows the clock. But i want to use it as a counter like a kronometre. It simply will start to count from zero if the page is refreshed. That i managed but i couldn't make it to count from zero. I know that something like below x = new date() ; y = new date(), start = x - y... but couldn't do it since my java knowledge is not enough. I will implement this into my php page. Can someone please help. It just has to start from 00:00:00 <html> <head> <script language="JavaScript"> <!-- var clockID = 0; function UpdateClock() { if(clockID) { clearTimeout(clockID); clockID = 0; } var tDate = new Date(); if((tDate.getSeconds())<=9) { document.theClock.theTime.value = "" + tDate.getHours() + ":" + tDate.getMinutes() + ":0" + tDate.getSeconds(); document.title = "The time is: " + tDate.getHours() + ":" + tDate.getMinutes() + ":0" + tDate.getSeconds(); } else if((tDate.getMinutes())<=9) { document.theClock.theTime.value = "" + tDate.getHours() + ":0" + tDate.getMinutes() + ":" + tDate.getSeconds(); document.title = "The time is: " + tDate.getHours() + ":0" + tDate.getMinutes() + ":" + tDate.getSeconds(); } else if(((tDate.getSeconds())<=9) && ((tDate.getMinutes())<=9)) { document.theClock.theTime.value = "" + tDate.getHours() + ":0" + tDate.getMinutes() + ":0" + tDate.getSeconds(); document.title = "The time is: " + tDate.getHours() + ":0" + tDate.getMinutes() + ":0" + tDate.getSeconds(); } else { document.theClock.theTime.value = "" + tDate.getHours() + ":" + tDate.getMinutes() + ":" + tDate.getSeconds(); document.title = "The time is: " + tDate.getHours() + ":" + tDate.getMinutes() + ":" + tDate.getSeconds(); } clockID = setTimeout("UpdateClock()", 1000); } function StartClock() { clockID = setTimeout("UpdateClock()", 500); } function KillClock() { if(clockID) { clearTimeout(clockID); clockID = 0; } } //--> </script> </head> <body onload="StartClock()" onunload="KillClock()"> <center> <form name="theClock"><input name="theTime" size="8" type="text"> </form> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
haku Posted August 7, 2009 Share Posted August 7, 2009 but couldn't do it since my java knowledge is not enough You won't find many people in the javascript forum who have strong java skills. Quote Link to comment Share on other sites More sharing options...
rugzo Posted August 8, 2009 Author Share Posted August 8, 2009 ok i got it <code><html> <head> <script language="JavaScript"> <!-- var clockID = 0; var saniye = 00; var dakika = 00; var saat = 00; function UpdateClock() { if(clockID) { clearTimeout(clockID); clockID = 0; } saniye ++; if(saniye <= 9) { var saniye_yaz = "0" + saniye ; }else{ var saniye_yaz = saniye ; } if (saniye > 59) { dakika ++; saniye = 0; saniye_yaz ="00"; } if(dakika <=9) { var dakika_yaz = "0" + dakika ; }else{ var dakika_yaz = dakika ; } if(dakika > 59) { saat ++; dakika = 0; dakika_yaz = "00"; } if(saat <=9) { var saat_yaz = "0" + saat ; }else{ var saat_yaz = saat ; } if(saat>=24) { saat=0; dakika=0; saniye=0; } document.theClock.theTime.value = saat_yaz + ":" + dakika_yaz + ":" + saniye_yaz; document.title = saat_yaz + ":" + dakika_yaz + ":" + saniye_yaz; clockID = setTimeout("UpdateClock()", 1000); } function StartClock() { clockID = setTimeout("UpdateClock()", 500); } function KillClock() { if(clockID) { clearTimeout(clockID); clockID = 0; } } //--> </script> </head> <body onLoad="StartClock()" onUnload="KillClock()"> <center> <form name="theClock"><input name="theTime" size="8" type="text"> </form> </center> </body> </html></code> 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.