Resume and Lap time on Stopwatch

You must Login before you can answer or comment on any questions.

Hello all, I am building a stopwatch application for Android and I am using JavaScript code. The basic functions of the stopwatch is to start, stop/resume, reset and lap time. Everything is working fine, but I am having some difficulty to make my LAP button function. Another minor problem I am having is with the STOP button. Although it does stop or pause the time, I also want it to resume the time. I am not sure on how to do this and I am wondering why doesn't my event listener fire for both the "Lap" and "Stop" buttons? I know this is probably very simple for most of you, but programming is not my strongest point. Any help will be appreciated. I have provided snippets of code below that is related to my question. "Button01" is the START button, "Button02" is the RESET button, "Button03" is the STOP button and "Button04" is the LAP button. Cheers.

var _startStopwatch = function() {
    timerlabel.value = '00:00:00.0';
 
 
var startTime = new Date();
 
var _updateTimer = function updateTimer() {
    var UNIT_HOUR = 60 * 60 * 1000;
    var UNIT_MINUTE = 60 * 1000;
    var UNIT_SEC = 1000;
    var now = new Date();
    var diff = now.getTime() - startTime.getTime();
    var hour = Math.floor(diff / UNIT_HOUR);
    var minute = Math.floor((diff - hour * UNIT_HOUR) / UNIT_MINUTE);
    var sec = Math.floor((diff - hour * UNIT_HOUR - minute * UNIT_MINUTE) / UNIT_SEC);
    var msec = Math.floor(diff % UNIT_SEC);
    timerlabel.text = ('0' + hour).slice(-2) + ':' + ('0' + minute).slice(-2) + ':' + ('0' + sec).slice(-2) + '.' + ('00' + msec).slice(-1);
    };
 
    intervalid = setInterval(_updateTimer, 1000);
    button03.title = 'STOP';
    };
 
    var _stopStopwatch = function() {
        clearInterval(intervalid);
    };
 
    var started = false;
    var intervalid = null;
    var resume = true;
 
button01.addEventListener("click", function(e){
    if (!started) {
        _startStopwatch();
        started = true;
      }
});
 
button02.addEventListener("click", function(e){
    if (started) {
        _stopStopwatch();
        started = false;
    }
    timerlabel.text = '00:00:00.0'; 
});
 
//STOP BUTTON
button03.addEventListener("click", function(e){
    if (started) {    
        _stopStopwatch();
        started = false;
    }
});
 
//LAP BUTTON NOT FUNCTIONING.
button04.addEventListener("click", function(e) {
    if (button01 == null | button03 == null) {
        return 'Undefined';
    } else {
        return(button03 - button01) / 1000;
    }
});

— asked 7 months ago by Bruce Ruffalo
1 Comment
  • One more thing, the main issue I am having is to make my event listener to fire in order to resume the time by clicking on the STOP button and clicking on the LAP button to lap times.

    — commented 7 months ago by Bruce Ruffalo

1 Answer

You really talk with this guy, he seems to have a similar issue that you are facing. Maybe he can give you some pointers.

Hope he can help you!

— answered 7 months ago by Christian Brousseau
answer permalink
3 Comments
  • Oh he's already asked! We're doing it as a group task. We both can't seem to figure it out.

    — commented 7 months ago by Bruce Ruffalo

  • So you're doing this as a "group task" and you don't know what your buddy is up to?

    I'm just saying.

    — commented 7 months ago by Christian Brousseau

  • I know what he's up to, but he didn't tell me that he would post the question up. I was originally supposed to do it.

    — commented 7 months ago by Bruce Ruffalo

Your Answer

Think you can help? Login to answer this question!