채택완료

SCRIPT 질문 드립니다.

2020-06-05 (금) 19:27:54 3,600
Copy
<?php
$a1 = '2020,06,05,19,20,00';
?>
<SCRIPT language=JavaScript> 

function getTime() { 

now = new Date(); 

dday = new Date(<?php echo $a1; ?>);  // D-Day 날짜, 시간 정확하게 초단위기입.

days = (dday - now) / 1000 / 60 / 60 / 24; 

daysRound = Math.floor(days); 

hours = (dday - now) / 1000 / 60 / 60 - (24 * daysRound); 

hoursRound = Math.floor(hours); 

minutes = (dday - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); 

minutesRound = Math.floor(minutes); 

seconds = (dday - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); 

secondsRound = Math.round(seconds); 

document.getElementById("counter1").innerHTML = hoursRound; 

document.getElementById("counter2").innerHTML = minutesRound; 

document.getElementById("counter3").innerHTML = secondsRound; 

newtime = window.setTimeout("getTime();", 1000); 

} 

</SCRIPT>

카운트 해주는 SCRIPT 입니다.

계속 반복되는 시간 카운트로 만들고 싶은데요...

마지막 00 분 00초 가 된 이후에 다시 3분 짜리 카운트가 돌아가게 하려고 합니다.

(이건 무한반복입니다.)

00:00 될때 D-day 값을 매번 디비에 넣었다가 다시 불러오고..그래야 할까요?

근데 그건 아닌거 같기도 하고...그럴려면 해당 SCRIPT 가 있는 페이지를 새로고침하기 전에는

소용없을거 같거든요..

어떻게 하는게 좋을까요...조언좀 부탁드립니다. ㅜ

답변 2개 / 댓글 3개

채택된 답변
+20 포인트
2020-06-05 (금) 21:00:18

새로 작성했어요. 초단위가 애를 먹이네요.

    <SCRIPT language=JavaScript>    
        var now = new Date();
        var dday = new Date();
        dday.setSeconds(dday.getSeconds()+5); // D-Day 날짜, 시간 정확하게 초단위기입.
        function getTime() {    
            var now = new Date().getTime();
            var distance = dday - now;
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);
            if (minutes<0) minutes = 0;
            if (seconds<0) seconds = 0;
            document.getElementById("counter1").innerHTML = hours;
            document.getElementById("counter2").innerHTML = minutes;
            document.getElementById("counter3").innerHTML = seconds;
            if( (minutes == '0') && (seconds == '0')){
                dday.setSeconds(dday.getSeconds()+5);
                console.log(dday);
            }
            //console.log(seconds);
        }
        var myVar = setInterval(getTime, 1000);
    </SCRIPT>

답변에 대한 댓글 2개

대단하십니다. 이상없이 0 까지 갔다가 다시 카운트 합니다.
감사합니다. ^^
https://codepen.io/sinbi/pen/abdOOjd
ps. 덕분에 저도 공부가 되었습니다.
2020-06-05 (금) 20:29:56

원래 소스를 최대한 활용해서

Copy
<SCRIPT language=JavaScript>    
        let now = new Date();
        var dday = new Date();
        dday.setSeconds(now.getSeconds()+5); // D-Day 날짜, 시간 정확하게 초단위기입.
        function getTime() {
            now = new Date();
            days = (dday - now) / 1000 / 60 / 60 / 24;
            daysRound = Math.floor(days);
            hours = (dday - now) / 1000 / 60 / 60 - (24 * daysRound);
            hoursRound = Math.floor(hours);
            minutes = (dday - now) / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
            minutesRound = Math.floor(minutes);
            seconds = (dday - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
            secondsRound = Math.ceil(seconds);
            document.getElementById("counter1").innerHTML = hoursRound;
            document.getElementById("counter2").innerHTML = minutesRound;
            document.getElementById("counter3").innerHTML = secondsRound;
            if( (minutesRound == '0') && (secondsRound == '0')  ){
                dday.setSeconds(now.getSeconds()+5);
                console.log(dday);
            }            
        }
        var myVar = setInterval(getTime, 1000);
    </SCRIPT>

답변에 대한 댓글 1개

답변 감사드립니다.
작성해 주신것을 유심히 보았는데요..

해당 SCRIPT 가 원소스도 마찬가지구..
카운트를 하는데...5..4..3..2..1..다음에 0 아니구 60 이 표기 되네요.

그러다보니 취미생활님이 작성해주신 if 구문의 secondsRound == '0' 을 지나치는듯 합니다. ㅜㅜ

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