The clock works correctly, the time shows as it should, Only they stand still and do not tick a second, when the page is updated, the clock is updated as it should, and time shows normally. The question is how to start the clock so that they start ticking? and seconds went in real time when you were on the site, the clock is already connected to the timing zone - there is no problem. Help to start the clock so that they go (the second began to move). Thank you very much. here is the code

<script type="text/javascript"> function clock() { var d = new Date(<?=date("Y");?>, <?=(date("m")-1);?>, <?=date("d");?>, <?=date("H");?>, <?=date("i");?>, <?=date("s");?>); var month_num = d.getMonth() var day = d.getDate(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); month=new Array("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"); if (day <= 9) day = "0" + day; if (hours <= 9) hours = "0" + hours; if (minutes <= 9) minutes = "0" + minutes; if (seconds <= 9) seconds = "0" + seconds; date_time = "Екатеринбург - " + day + " " + month[month_num] + " " + d.getFullYear() + " г.&nbsp;&nbsp;&nbsp;"+ hours + ":" + minutes + ":" + seconds; if (document.layers) { document.layers.doc_time.document.write(date_time); document.layers.doc_time.document.close(); } else { document.getElementById("doc_time").innerHTML = date_time; } setTimeout("clock()", 1000); } </script> <script type="text/javascript"> clock(); </script> 
  • If I'm not mistaken, only the function name without brackets is passed to the setTimeOut function. Remove them, should earn: setTimeout("clock", 1000); - Alexander Gribennikov

1 answer 1

 if (document.layers) { 

Is it from the book of the year 97-99? The layers property for many years has not been supported by all available browsers and is Netscape specific (yes, there was such a browser).

I corrected the code a little, instead of

 if (document.layers) { document.layers.doc_time.document.write(date_time); document.layers.doc_time.document.close(); } else document.getElementById("doc_time").innerHTML = date_time; setTimeout("clock()", 1000); } 

need to write like this

 document.getElementById("doc_time").innerHTML = date_time; setTimeout(clock, 1000); 

And do not forget to check that there is an id="doc_time" tag

  • I prescribe so if (document.layers) {document.layers.doc_time.document.write (date_time); document.layers.doc_time.document.close (); } document.getElementById ("doc_time"). innerHTML = date_time; setTimeout (clock, 1000); } - user206450
  • when I remove if (document.layers) {document.layers.doc_time.document.write (date_time); document.layers.doc_time.document.close (); } the watches are no longer displayed - user206450
  • that is, when the code is written like this date_time = "Ekaterinburg -" + day + "" + month [month_num] + "" + d.getFullYear () + "g. & nbsp; & nbsp; & nbsp;" + hours + ":" + minutes + ":" + seconds; document.getElementById ("doc_time"). innerHTML = date_time; setTimeout (clock, 1000); </ script> <script type = "text / javascript"> clock (); the clock simply ceases to be selected, naturally the id doctime tag is registered where necessary and displayed correctly - user206450
  • if you just delete if (document.layers) {document.layers.doc_time.document.write (date_time); document.layers.doc_time.document.close (); } - the clock ceases to show voosche and on the place where there should be a clock - emptiness. Help plz. - user206450