-3

Is there a way to pause and resume a timer ?

I want to create something like "worker time-keeping"

When he start the day call timer.start()

When he takes a break call timer.pause()

When he resumes a break call timer.resume()

When he ends the day call timer.stop()

And at the and I can see the total number of minutes/hours/whatever.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Roland
  • 885
  • 3
  • 12
  • 16
  • 1
    `timer.start()` -> write start time. `timer.pause()` -> write stop time. `timer.resume()` -> write start time. `timer.stop()` -> write stop time. Also on `stop()` sum up each start/stop pair time spans. Calculate minutes/hours/whatever from that total. You can also keep a total on `resume()` instead of calculating at the end. – VLAZ Jul 02 '21 at 10:21

1 Answers1

1

The preferred way of doing this is to save the time at which he starts, pauses, resumes, and stops.

Afterwards, you can calculate the total time the worker worked by calculating the difference in time between these saved times.

Example:

  1. Worker clocks in at 8:00 -> save 8:00 as start time
  2. Worker pauses at 12:00 -> save 12:00 as pause time
  3. Worker resumes at 13:00 -> save 13:00 as resume time
  4. Worker stops at 18:00 -> save 18:00 as stop time

Now to calculate how much the worker worker you just take the difference between 8:00 and 12:00 and add that to the difference between 13:00 and 18:00

Ian
  • 431
  • 2
  • 7
  • ok, so let's say I use `Date.now()` and after 10 minutes I write `Date.now()` again. And then I calculate the difference and get something like `87733`. How do I know how many hours/minutes/seconds are these? – Roland Jul 02 '21 at 11:07
  • 1
    @Roland with simple arithmetic. The value is in milliseconds, divide by 1000 and you'll get the seconds. Divide by another 60 you'll get the minutes. Divide by another 60, you'll get the hours. [JavaScript - Get minutes between two dates](https://stackoverflow.com/q/7709803) – VLAZ Jul 02 '21 at 11:20