0

I have this html code, and im trying to get a website where everyone can see the chronometer and change it at any time.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Chronometer</title>

    <style>
        body {
            font-family: monospace;
            font-size: 2em;
            padding-top: 1em;
            text-align: center
        }
    </style>
</head>
<body>

    <h1 data-chronometer>00:00:00</h1>
    <button id="play">play</button>
    <button id="pause">pause</button>

    <script type="text/javascript">

        let hours = `00`,
            minutes = `00`,
            seconds = `00`,
            chronometerCall

        function chronometer() {

            seconds++

            if (seconds < 10) seconds = `0`+seconds

            if (seconds > 59) {
                seconds = `00`
                minutes ++

                if (minutes < 10) minutes = `0`+minutes
            }

            if (minutes > 59) {
                minutes = `00`
                hours ++
            }

            document.querySelector(`[data-chronometer]`)
                    .textContent = `${hours}:${minutes}:${seconds}`

        }

        play.onclick = (event) => {
            chronometerCall = setInterval(chronometer, 1000)
            event.target.setAttribute(`disabled`,``)
        }

        pause.onclick = () => {
            clearInterval(chronometerCall)
            play.removeAttribute(`disabled`)
        }


    </script>
</body>
</html>

I need to save the time in a MySQL database (Table T_Time with time) so everyone can see the same time in every PC. I'm a little lost and don't really know what to do.

Thanks

  • 3
    you need a back-end language like PHP for this. – AD-1 Feb 11 '21 at 07:57
  • 1
    I don't understand the objective. But it sounds suspiciously like reinventing the wheel. – Strawberry Feb 11 '21 at 08:17
  • Backend php or nodejs my sql db connection code required to perform the complete this task. If you are using nodejs as backend then use ajax in client side js to call node js api which aacepts time as input and insert it into table – Sudhir S Pawar Feb 11 '21 at 08:31
  • Ideal answer: ```learn back-end language like php / python (flask/django) / java (jsp) / nodejs```. Answer if it have to be javascript: ```learn nodejs```. Answer if you're lazy: ```use firebase cloud firestore``` – Kristian Feb 11 '21 at 08:41

1 Answers1

0

You can try this JavaScript code for getting current time

var d = new Date();
var time = d.getHours() +":"+ d.getMinutes() +":"+ d.getSeconds();
console.log(time);

You can append this script with your code but first you have to modify it w.r.t your html code and you can also read more about date and time function on w3 schools website here is a link for it :- https://www.w3schools.com/js/js_date_methods.asp

Mehul Jadhav
  • 1
  • 1
  • 2