timer - Pause/Resume javascript Pomodoro clock -


i've been working on javascript pomodoro clock. able set session time , break time , counts down without trouble. reason can not pause , resume work. when timer starts capture date.now() , when pause capture current date.now(). find difference , subtract duration, hoping resume @ paused time, still keeps subtracting additional seconds. code (from codepen) below

$(document).ready(function() {    var total;    var i;    var x;    var y;    var display;    var minutes;    var seconds;    var duration;    var sessioninterval;    var freeze;    var timepast;    var t;    var start;    var clock;      function timer(end) {      total = date.parse(end) - date.parse(new date());      minutes = math.floor((total / 1000 / 60) % 60);      seconds = math.floor((total / 1000) % 60);      return {        'total': total,        'minutes': minutes,        'seconds': seconds      };    }      function begintimer() {      start = date.now();      clearinterval(sessioninterval);      clock = document.getelementbyid('display2');      start = date.now();      sessioninterval = setinterval(function() {        t = timer(duration);        clock.innerhtml = 'minutes:' + t.minutes + '<br>' + 'seconds:' + t.seconds + '<br>';          if (t.total <= 0) {          clearinterval(sessioninterval);            if (i === 0) {            session();            } else if (i === 1) {            breaktime();          }        }      }, 1000);    }      function session() {      duration = new date(date.parse(new date()) + (x * 60 * 1000));      begintimer();      = 1;    }      function breaktime() {      duration = new date(date.parse(new date()) + (y * 60 * 1000));      begintimer();      = 0;    }      $(".sendinput").click(function() {        if (x == null) {        x = 25;        } else {        x = parseint(document.getelementbyid("worktime").value, 10);      }        if (y == null) {        y = 5;        } else {        y = parseint(document.getelementbyid("breakmin").value, 10);      }      session();    });      $(".sendpause").click(function() {      freeze = date.now();      timepast = freeze - start;      clearinterval(sessioninterval);    });      $(".sendresume").click(function() {      if (i === 1) {        duration = new date(((date.parse(new date())) + (x * 60 * 1000)) - timepast);      }        if (i === 0) {        duration = new date(((date.parse(new date())) + (y * 60 * 1000)) + timepast);      }        begintimer();    });    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <input type="text" placeholder="break: 5 minutes" id="breakmin">      <input type ="text" placeholder="session: 25 minutes" id="worktime">      <input type="button" value="start" class="sendinput">       <input type="button" value="pause" class="sendpause">      <input type="button" value="resume" class="sendresume">      <div id="display2">  </div>

the major logic problem within resume function not reset start new notional value timepast milliseconds before present. using original start value after pause of undetermined duration not work.

date.parse(new date()) appeared causing problems. without spending time on debugging further, all occurrences of date.parse(new date()) replaced date.now().

so cleaned version of resume function appears work:

  $(".sendresume").click(function() {     var = date.now();     if (i === 1) {       duration = new date( + x * 60 * 1000 - timepast);     }      if (i === 0) {       duration = new date( + y * 60 * 1000 + timepast);     }      begintimer();     start = - timepast;  // <-- reset notional start time   }); 

but please test further - may wish investigate why timepast added in 1 calculation of duration , subtracted in other!


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -