Simple JavaScript clock

18.04.19

Although this code can easily be found on the Internet, we still write a simple JavaScript clock.

Clock:

 
<div id="clock"></div>
<script>
    window.onload = clock;
    function clock(){
        let date = new Date(),
            hours = date.getHours(),
            minutes = date.getMinutes(),
            seconds = date.getSeconds();
        if (hours < 10)
            hours = '0' + hours;
        if (minutes < 10)
            minutes = '0' + minutes;
        if (seconds < 10)
            seconds = '0' + seconds;
        let str = hours + ':' + minutes + ':' + seconds;
        document.getElementById('clock').innerHTML = str;
        setTimeout(() =>{
            clock();
        }, 1000);
    }
</script>​
blog comments powered by Disqus