Event listener problems in function

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

Hey, I was wondering if anyone would be able to help me with the event listeners in this function.. They seem to not be working and I'm absolutely clueless :/

function startingStopwatch() {

lapStopwatch.addEventListener('click', lappingStopwatch);
stopStopwatch.addEventListener('click', stoppingStopwatch);

winStopwatch.add(lapStopwatch);
winStopwatch.add(stopStopwatch);

var startTime = new Date().getTime();
swatch = 1;

while(swatch == 1) {

    var newTime = new Date().getTime();

    hours = (newTime - startTime) / 3600000;
        min = ((newTime - startTime) % 3600000) / 60000;
        sec = (((newTime - startTime) % 3600000) % 60000) / 1000;
    millisec = ((((newTime - startTime) % 3600000) % 60000) % 1000) / 10;

    if(hours >= 1) {
        if(hours < 10) {
            stopwatchHour.text = "0" + Math.floor(hours);
        }
        else {
            stopwatchHour.text = Math.floor(hours);
        }
    }
    else {
        stopwatchHour.text = "00";
    }

    if(min >= 1) {
        if(min < 10) {
            stopwatchMin.text = "0" + Math.floor(min);
        }
        else {
            stopwatchMin.text = Math.floor(min);
        }
    }
    else {
        stopwatchMin.text = "00";
    }

    if(sec >= 1) {
        if(sec < 10) {
            stopwatchSec.text = "0" + Math.floor(sec);
        }
        else {
            stopwatchSec.text = Math.floor(sec);
        }
    }
    else {
        stopwatchSec.text = "00";
    }

    if(millisec >= 1) {
        if(millisec < 10) {
            stopwatchMs.text = "0" + Math.floor(millisec);
        }
        else {
            stopwatchMs.text = Math.floor(millisec);
        }
    }
    else {
        stopwatchMs.text = "00";
    }
}

}

2 Answers

It would help if you also posted the code for the listeners that aren't working: lappingStopwatch() and stoppingStopwatch().

I was playing around with it and as soon as I take the while loop out of the code, they work fine. I don't know how to combat this problem..

Here are the listeners. Cheers

function stoppingStopwatch() { swatch = 0; winStopwatch.close(stopStopwatch); winStopwatch.close(lapStopwatch); }

function lappingStopwatch() { if(lapNum == 1) { lap1.text = "1. " + hours + ":" + min + ":" + sec + ":" + millisec; winStopwatch2.add(lap1); lapNum = 2; }

else if(lapNum == 2) {
    lap2.text = "2. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap2);
    lapNum = 3;
}

else if(lapNum == 3) {
    lap3.text = "3. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap3);
    lapNum = 4;
}

else if(lapNum == 4) {
    lap4.text = "4. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap4);
    lapNum = 5;
}

else if(lapNum == 5) {
    lap5.text = "5. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap5);
    lapNum = 6;
}

else if(lapNum == 6) {
    lap6.text = "6. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap6);
    lapNum = 7;
}

else if(lapNum == 7) {
    lap7.text = "7. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap7);
    lapNum = 8;
}

else if(lapNum == 8) {
    lap8.text = "8. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch.add(lap8);
    lapNum = 9;
}

else if(lapNum == 9) {
    lap9.text = "9. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap9);
    lapNum = 10;
}

else if(lapNum == 10) {
    lap2.text = "10. " + hours + ":" + min + ":" + sec + ":" + millisec;
    winStopwatch2.add(lap10);
    lapNum = 11;
}

else {

}

Your Answer

Think you can help? Login to answer this question!