Friday 8 June 2018

Javascript - Dynamic Blog - Clock

This page is just to host a clock. This is part of an Chrome Extension blog post to be published soon. This page demonstrates that blogspot can run javascript code making pages more dynamic.

14:27:34

The code to make this clock tick is to be found in a <script> tag....

    <script>
        ~function () {
            'use strict';
            startTime();
  
        }();


        function startTime() {
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = padZero(m);
            s = padZero(s);
            document.getElementById('clock').innerHTML =
                h + ":" + m + ":" + s;
            var t = setTimeout(startTime, 1000);
        }
        function padZero(i) {
            if (i < 10) { i = "0" + i };  // add zero in front of numbers < 10
            return i;
        }
    </script>

No comments:

Post a Comment