내 웹 페이지에 현재 1 시간으로 설정된 카운트 다운 타이머가 표시되어야합니다. 나는 페이지를 떠날 때에도 타이머가 카운트 다운을 계속할 수 있도록 쿠키에 카운트 다운을 저장하려고합니다. 나는 javascript에서 쿠키를 사용하는 초보자이므로 여기서 내가 잘못하고있는 것에 대한 도움이 정말로 필요하다.카운트 다운 타이머를 쿠키에 저장하면 페이지를 떠나거나 다시로드 할 때도 계속 카운트 다운됩니다.
여기에 지금은 카운트 다운 대신 자체를 표시하는, 나는 그것이 온 아무 생각이 어떤 임의의 텍스트를 표시 내 HTML 코드
<b id="timer">00:00:00</b><br>
<button id="start">Start</button>
<button id="stop" hidden>Stop</button>
자바 스크립트
var counter=0;
var timeleft=3600;
//function that returns countdown format 00:00:00
function convertSeconds(s){
var hr=Math.floor(s/3600);
var min=Math.floor(s/60%60);
var sec=s%60;
return ("0" + hr).slice(-2) + ':' + ("0" + min).slice(-2) + ':' + ("0" + sec).slice(-2);
}
//cookie expire time variable set to 1hr
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
var cd = document.getElementById("timer");
if(document.cookie.length != 0){
var carray = document.cookie.split("=");
cd.innerHTML=carray[1]; //should display initial countdown duration before btnstart is clicked
function timeIt(){ //function that displays the dynamic countdown timer
counter++;
cd.innerHTML=carray[1];
}
}
var btnstart = document.getElementById("start");
var btnstop = document.getElementById("stop");
//starts countdown when btnstart clicked
btnstart.onclick = function()
{
//storing the countdown format to a cookie and setting expire time to 1hour
document.cookie = "cdanim=" + convertSeconds(timeleft-counter); + ";expires=" + now.toUTCString();
setInterval(timeIt, 1000); //starts the countdown
btnstart.hidden=true;
btnstop.hidden=false;
}
입니다. carray [1]는 1:00:00을 반환해야하지만 그렇지는 않습니다. 도와주세요.