I'm working on a Pomodoro-esque timer. The timer must be able to do the following:
When 'Start' button is pressed:
- Start 5 second timer. Times will change, of course)
- After timer expires, send AJAX event with start and end timestamp
- Start a 3 second timer.
- After timer expires, start a new 5 second timer. (Loop step 1 to 3 a total of FOUR times)
- At the end of the fourth loop, start a 7 second timer.
- After the 7 second timer expires, start the 5-3 loop again.
When 'Stop' button is pressed:
- Cancel the timer, reset it back to the default state.
This is the code I have now:
HTML
<html>
<head>
    <meta charset="UTF-8">
    <title>Pomodoro Test</title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div id="test"></div>
<div id="timer">5</div>
<button id="start">Start</button>
<button id="reset">Reset</button>
<div id="startTime"></div>
<div id="endTime"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="functions.js"></script>
</body>
</html>
JavaScript:
var timer = 5;
var flag = 0;
var startTime;
var endTime;
var intervalTimer;
function update() {
  if (timer-- > 0) {
    $("#timer").html(timer); // retrieve value from html
  } else {
    endTime = new Date();
    //alert("Start Time = " + startTime + "\nEnd Time = " + endTime);
    $("#endTime").html("End time: " + endTime.toTimeString());
    resetTimer();
    // TODO: send ajax call to server, sending startTime and endTime
    // so that these can be saved to database
  }
}
function loopingTimer() {
  while(flag === 0) {
    startTime = new Date();
    $("#startTime").html("Start time: " + startTime.toTimeString());
    startTimer();
    i++;
  setTimeout(function() {
    if(i < 5) {
      loopingTimer();
    }
  }, 3001)}};
$("#start").click(function() {
  loopingTimer();
});
$("#reset").click(function() {
    flag = 1;
  });
function startTimer() {
  intervalTimer = window.setInterval(update, 500);
}
function resetTimer() {
  stopTimer();
  timer = 5;
  $("#timer").text(timer);
}
function stopTimer() {
  window.clearInterval(intervalTimer);
}
I find it extremely difficult to get the desired functionality, I've been tinkering with this code for 2 days. I sincerely hope you guys can help me out.
Cake.
 
    