채택완료

다중 타이머 질문

2020-08-31 (월) 18:27:54 3,470

3731115165_1598865898.7588.png

타이머가 여러개 있고 한개의 스크립트로 돌리면

킬때마다 시간이 빨라지고 한개가 끝나면 멈춰버리는 현상이 있습니다.

이런경우에는 어떻게 해결 해야 될까요..

Copy
<script>

    time =  300; //45*60*10;

    var running = 0;

 

function increment(op,sp){

    if(running == 1){

        setTimeout(function(){

            time--;

            var mins = Math.floor(time/10/60);

            var secs = Math.floor(time/10 % 60);

            var hours = Math.floor(time/10/60/60); 

            var tenths = time % 10;

            if(mins < 10){

                mins = "0" + mins;

            } 

            if(secs < 10){

                secs = "0" + secs;

            }

            if(time <= 0) {

                //result();

                Myaudio.play();

                document.getElementById(sp).style.backgroundColor = "red";  

                document.getElementById(sp).style.borderColor = "red";

                reset();

                

                    }

            document.getElementById(op).innerHTML = mins + ":" + secs ;

            

            increment(op,sp);

        },100)

    }

}

function startPause1(ch,op,sp){

 

     running = 1;

    increment(op,sp);

    document.getElementsByClassName(ch).innerHTML = "Pause";

    document.getElementById(sp).style.backgroundColor = "green";    

    document.getElementById(sp).style.borderColor = "green";

}

 

function reset(op,sp){

running = 0;

time =  300; //45*60*10;

document.getElementsByClassName(ch).innerHTML = "Start";

document.getElementById(op).innerHTML = "00:00";

document.getElementById(sp).style.backgroundColor = "red";  

document.getElementById(sp).style.borderColor = "red";

}

</script>

답변 3개 / 댓글 3개

채택된 답변
+20 포인트
2020-09-01 (화) 10:54:06

변수 time, running을 같이 사용하는군요. 개별 사용하도록 바꾸면 되겠네요.

3731115165_1598887285.3326.gif 

1개 만 누를경우 정상적으로 이용가능한데

2개이상 누를경우 오작동이 발생되됩니다.

도움이 필요합니다..ㅠㅠ

increment 함수가 재귀함수로 되어 있어서 그런거 아닌가요?

답변에 대한 댓글 3개

어떻게 해결하면 좋을까요...초보라
function increment(op,sp) 함수안에 있는 increment(op,sp) 호출문을 지워 보세요.
increment 안에 increment 지우면 1초만 적용되고 멈춰버려요...ㅜㅜ

답변을 작성하려면 로그인이 필요합니다.