我正在尝试在jquery中创建一个计时器,当页面刷新并且我遇到麻烦时它会持续存在。计时器工作,但当我刷新页面时,它会消失,直到计时器从最初的30秒重置,并在刷新时再次出现,我正在使用php文件。任何帮助将不胜感激。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<script>
var miliseconds = 30000;
function reset() {
localStorage.timer = +new Date + miliseconds;
}
if( ! localStorage.timer ) {
reset();
}
setInterval( function() {
var remaining = localStorage.timer - new Date;
if( remaining >= 0 ) {
$('#timer').text( Math.floor( remaining / 1000 ) );
} else {
reset();
}
}, 300 );</script>
<body>
<div>Will be ready in <span id="timer"></span></div>
</body>