0

이 페이지를 새로 고친 후 다시 시작하지 않는 카운트 다운을하려고합니다. 키스 우드 카운트 다운을 사용하고 있습니다. 5 분까지 계산하고 싶습니다.페이지 새로 고침에서 계속 나타나는 자바 스크립트 카운트 다운

var countdownTarget = localStorage.getItem('countdownTarget'); 
    if (countdownTarget === null) 
    { 
     var countdownTarget = new Date(); 
     countdownTarget = new Date(countdownTarget.getFullYear(), countdownTarget.getMonth(), countdownTarget.getDate(), countdownTarget.getHours(), countdownTarget.getMinutes() + 5, countdownTarget.getSeconds()); 
     localStorage.setItem('countdownTarget', countdownTarget); 
    } 

    $('#defaultCountdown').countdown({ until : countdownTarget, format: 'MS', compact: true}); 

브라우저의 로컬 저장소에 현재 날짜를 5 분 남겨두고 countdownTarget (이전에 완료되지 않은 경우에만)로 저장합니다. 그리고 사용자가 페이지를 새로 고치면 시간은 지속적이어야합니다. 그러나 문제가 있습니다. 페이지를 처음로드 할 때 시간은 5 분 (정상이어야 함)부터 시작됩니다.하지만 다시 새로 고치려고 할 때 36 분부터 계산되기 시작합니다. 이유는 모르지만 모르겠습니다. 이 36 분간의 출전 시간.

+0

36 분은 어디에서 왔습니까? 코드 예제에서 이와 같은 것을 볼 수 없습니다. –

+0

당신은 아마도 count = null로 말하면 countdownTarget을 그늘지게하는 것을 의미하지 않습니다. var = – aw04

+1

저는 localStorage가 데이터를 문자열로 저장한다는 것을 확신합니다. localStorage에서 저장된 문자열을로드 한 다음 int에 추가하면 예상 결과를 얻지 못할 것입니다. 카운터에 추가하기 전에 저장된 문자열을 다시 int로 변환해야합니다. – Korgrue

답변

1

날짜를 다시 선언하는 대신 분을 증가시키지 않는 이유는 무엇입니까? 귀하의 코드가 나를 위해 일했습니다. 또한 countdownTarget 만 한 번 시작해야합니다.

var countdownTarget = localStorage.getItem('countdownTarget'); 
if (countdownTarget === null) { 
    countdownTarget = new Date(); 
    countdownTarget.setMinutes(countdownTarget.getMinutes() + 5); 
    localStorage.setItem('countdownTarget', countdownTarget); 
} else { 
    countdownTarget = new Date(countdownTarget); 
} 
$('#defaultCountdown').countdown({ until : countdownTarget, format: 'MS', compact: true}); 
관련 문제