I am having difficulty to lap/split time in my Stopwatch. I have given it a try and it doesn't work. I am trying to get the event listener to fire for my Lap button. Can anyone help me out? If you need more code let me know because I am just providing a portion of it for this question. I have this due really soon. Thanks in advance!
var laps = 1; var _startStopwatch = function() { timerlabel.value = '00:00:00.0'; var startTime = new Date(); var stopTime = startTime; 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); }; var _stopStopwatch = function() { clearInterval(intervalid); } var _resumeStopwatch = function(){ clearInterval(intervalid); var startTime = new Date(); } var started = false; var intervalid = null; var totaltime = 0; var startTime = new Date(); var now = new Date(); var diff = now.getTime() - startTime.getTime(); var stopwatch; //LAP FUNCTION var _marklap = function(){ if(started == 1) { if(lapdate != '') { var lapcounter = 0; var lapdate = ''; var lapdetails; var lapold = lapdate.split(':'); var lapnow = stopwatch.value.split(':'); var lapcount = new Array(); var x = 0 for(x;x<lapold.length;x++) { lapcount[x] = new Array(); lapcount[x][0] = lapold[x]*1; lapcount[x][1] = lapnow[x]*1; } if(lapcount[1][1] < lapcount[1][0]) { lapcount[1][1] += 60; lapcount[0][1] -= 1; } if(lapcount[2][1] < lapcount[2][0]) { lapcount[2][1] += 10; lapcount[1][1] -= 1; } var mzeros = (lapcount[0][1] - lapcount[0][0]) < 10?'0':''; var szeros = (lapcount[1][1] - lapcount[1][0]) < 10?'0':''; lapdetails.value += '\t+' + mzeros + (lapcount[0][1] - lapcount[0][0]) + ':' + szeros + (lapcount[1][1] - lapcount[1][0]) + ':' + (lapcount[2][1] - lapcount[2][0]) + '\n'; } lapdate = stopwatch.value; lapdetails.value += (++lapcounter) + '. ' + stopwatch.value; } } //LAP BUTTON button04.addEventListener("click", function(e) { if(started){ _marklap(); } });
Your Answer
Think you can help? Login to answer this question!