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; } });
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!
Your Answer
Think you can help? Login to answer this question!