js
JS - 카운트다운 타이머
카운트다운 타이머입니다.
일정 시간이 초과되면 "세션 만료"라는 문구가 출력됩니다.
기본값은 "10(분)"입니다.
<script type="text/javascript">
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var interval = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
timer = duration;
}
if(timer === 0) {
clearInterval(interval);
display.textContent = "세션 만료!";
}
}, 1000);
}
window.onload = function () {
/* 기본값 10(분)입니다. */
var minutes = 10;
var fiveMinutes = (60 * minutes) - 1,
display = document.querySelector('#MyTimer');
startTimer(fiveMinutes, display);
};
/* 출처: https://stackoverflow.com/questions/9755706/javascript-countdown-timer-for-session-timeout/54370803 */
</script>
<span id="MyTimer">10:00</span>
아래는 실제 적용한 결과입니다.
10:00
2 댓글
감사합니다. 딱 제가 원하던 기능이라 그런데 블로그에 퍼가도 될까요?
답글삭제네~ 제한이 없습니다.
삭제