Time Tracking on mouse click Javascript

Alex Toma

I wanted to make a really simple time tracker that runs in a browser. This is the simplest code I could come up with but I have problems with the click event. The problem is that the whole thing works but it starts on page load and I want it to start on click. Is there anything wrong with the implementation?

Here is my code:

let hours = 0;
let minutes = 0;
let seconds = 0;
function time() {
  setInterval(function () {
    if (seconds < 59) {
      seconds++;
      document.getElementById("timer").textContent =
        hours + ":" + minutes + ":" + seconds;
    } else if (minutes < 59) {
      seconds = 0;
      minutes++;
      document.getElementById("timer").textContent =
        hours + ":" + minutes + ":" + seconds;
    } else {
      hours++;
      document.getElementById("timer").textContent =
        hours + ":" + minutes + ":" + seconds;
    }
  }, 1000);
}

document.getElementById("button").addEventListener("click", time());
body {
  background-color: blanchedalmond;
}
div {
  margin-top: 200px;
  display: flex;
  align-items: center;
  flex-direction: column;
}
#timer {
  min-height: 80px;
  font-size: 4em;
}
h1 {
  display: block;
}
#button {
  margin-top: 50px;
  width: 180px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="script.js"></script>
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
      crossorigin="anonymous"
    />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="a">
      <h1 id="header">You have been working for:</h1>
      <time id="timer">0:0:0</time>
      <button id="button" class="btn btn-primary">Track Time</button>
    </div>
  </body>
</html>

Max

The problem is that you call the time function when you add an event listener. In order to run the time function only when the button is clicked, just don't call the time function right away:

document.getElementById("button").addEventListener("click", time);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

real time mouse tracking via the external js

Mouse tracking eyes logo animation - javascript

Javascript mouse click coordinates for image

Simulate mouse click on button in JavaScript

Tracking mouse coordinates in Qt

Tracking the mouse movement

Tracking the mouse position on the QGraphicsItem

How to simulate a mouse click using JavaScript?

Mouse click simulates keyboard key in JavaScript

How to make mouse double click in JavaScript?

Adding mouse click event with an array of objects in JavaScript

Simulate a REAL HUMAN mouse click in pure javascript?

Trigger mouse click by window offset in javascript

Javascript Ckeditor Get Mouse click position

how do you click where the mouse is with javascript

Javascript - get time of link click

Tracking the Date time changes in DatePicker control using Javascript/JQuery

Getting mouse coordinates in SDK first-time right-click

Change time selected on mouse click from 30 minutes to an hour

Jquery focusout runs several time by each mouse out click

How to detect both left and right mouse click at the same time in Pygame

Disappear object on mouse click on that object in javascript. In my case I want to disappear a bouncing ball on the click of the mouse

Button Click Tracking

tracking mouse position on Java WorldWind

OpenGL Mouse Tracking (Freeform Line)

Is there a short way for adding mouse click sound every time mouse clicked in my project?

Is it possible to differentiate between a button click triggered by a mouse click and a click triggered by the user's javascript?

JavaScript or jQuery event handlers for "Ctrl"/"Shift" + mouse left button click

Is there a way to simulate pressing multiple keys on mouse click with javascript?