0

I created a clock in Javascript, but I would like it to refresh automatically. I added a line: setInterval (data_czas, 1000); but if I gave it, the clock was automatically refreshed, but everything on my side disappeared and the clock itself was left, where every now and then there was a line with a new, refreshed date. Please help.

> <script>
function data_czas() {
    var miesiace = ["Stycznia","Lutego","Marca","Kwietnia","Maja","Czerwca","Lipca","Sierpnia","Września","Października","Listopada","Grudnia"];
    var dni = ["Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota","Niedziela"];
    var data = new Date();
    var rok = data.getFullYear();
    var miesiac = data.getMonth();
    var tydzien = data.getDate();
    var dzien = data.getDay();
    var godzina = data.getHours();
    var minuta = data.getMinutes();
    var sekunda = data.getSeconds();
    if (minuta < 10) {
    minuta = "0" + minuta;
    }
    if (sekunda < 10) {
    sekunda = "0" + sekunda;
    }
    var p_data_czas = dni[dzien] + ", " + tydzien + " " + miesiace[miesiac] + " " + rok + " roku <br>" + "Godzina: " + godzina + ":" + minuta + ":" + sekunda
    document.write(p_data_czas);
    }
    document.write("<b> Dzisiaj jest: <Br>");
    data_czas();
    document.write("</b>")
</script>
</b>
kmoser
  • 8,780
  • 3
  • 24
  • 40
STIVI999
  • 1
  • 1
  • 1
    You should not use document.write. You need to look into innerHTML so you can replace the content without overwriting the page. You also need to use setTimeout or setInterval – epascarello Feb 24 '22 at 20:42
  • Where is your call to `setInterval()`? – kmoser Feb 24 '22 at 20:44
  • 1
    You may want to read [Why is document.write considered a "bad practice"?](https://stackoverflow.com/q/802854/215552) – Heretic Monkey Feb 24 '22 at 20:44
  • I added a line: setInterval (data_czas, 1000); but if I gave it, the clock was automatically refreshed, but everything on my side disappeared and the clock itself was left, where every now and then there was a line with a new, refreshed date – STIVI999 Feb 25 '22 at 16:17

0 Answers0