当用户在网页中导航到不同的路径时,如何在JavaScript中存储计数计时器
我正在尝试构建一个网页,该网页允许您在单击“开始”按钮时启动一个计数计时器,并允许您导航到网页上的不同路线启动另一个计时器,然后导航回第一个页面并使计时器仍在运行。目前,我有和按钮的功能工作。然而,我不知道如何让时钟不复位。当我导航到另一个页面时。这个网页主要是用Python和Flask编写的,但我使用JavaScript来实现时间功能。这是我使用过的唯一一个JavaScript,大部分来自其他帖子当用户在网页中导航到不同的路径时,如何在JavaScript中存储计数计时器,javascript,flask,Javascript,Flask,我正在尝试构建一个网页,该网页允许您在单击“开始”按钮时启动一个计数计时器,并允许您导航到网页上的不同路线启动另一个计时器,然后导航回第一个页面并使计时器仍在运行。目前,我有和按钮的功能工作。然而,我不知道如何让时钟不复位。当我导航到另一个页面时。这个网页主要是用Python和Flask编写的,但我使用JavaScript来实现时间功能。这是我使用过的唯一一个JavaScript,大部分来自其他帖子 :: 开始 暂停 重置 设小时=0; 设分钟=0; 设秒=0; 让totalSeconds=0
::
开始
暂停
重置
设小时=0;
设分钟=0;
设秒=0;
让totalSeconds=0;
设intervalId=null;
函数startTimer(){
++总秒数;
小时=数学楼层(总秒数/3600);
分钟=数学地板((总秒-小时*3600)/60);
秒=总秒-(小时*3600+分钟*60);
document.getElementById(“hour”).innerHTML=hour;
document.getElementById(“分钟”).innerHTML=minute;
document.getElementById(“秒”).innerHTML=seconds;
}
document.getElementById('start-btn')。addEventListener('click',()=>{
intervalId=setInterval(起始时间,1000);
})
document.getElementById('stop-btn')。addEventListener('click',()=>{
如果(有效期内)
clearInterval(intervalId);
});
document.getElementById('reset-btn')。addEventListener('click',()=>{
总秒数=0;
document.getElementById(“hour”).innerHTML='0';
document.getElementById(“分钟”).innerHTML='0';
document.getElementById(“秒”).innerHTML='0';
});
如果要防止计时器重置,请使用cookie存储计时器启动的日期
<!DOCTYPE html>
<html>
<head>
<title>Timer test</title>
</head>
<body>
<div id="time">
<div><span id="hour"></span>:<span id="minute"></span>:<span id="seconds"></span></div>
<button id="start-btn">Start</button>
<button id="stop-btn">Pause</button>
<button id="reset-btn">Reset</button>
</div>
<script type="text/javascript">
let hour = 0;
let minute = 0;
let seconds = 0;
let pauseTime = null;
let start = [...document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x => x[1].trim() == 'timer').map(x => Number(x[2].trim()))[0];
if(start != null) {
if(start > 0) start = new Date(start);
else if(start < 0) {
pauseTime = -start;
start = new Date(Date.now() + start); //+- = -
} else start = null;
} else start = null;
let intervalId = null;
function startTimer() {
let totalSeconds;
if(pauseTime) {
start = new Date(Date.now() - pauseTime);
totalSeconds = pauseTime;
return;
} else {
totalSeconds = Math.floor((Date.now() - start.getTime()) / 1000);
}
hour = Math.floor(totalSeconds /3600);
minute = Math.floor((totalSeconds - hour*3600)/60);
seconds = totalSeconds - (hour*3600 + minute*60);
document.getElementById("hour").innerHTML =hour;
document.getElementById("minute").innerHTML =minute;
document.getElementById("seconds").innerHTML =seconds;
}
if(start) intervalId = setInterval(startTimer, 1000);
document.getElementById('start-btn').addEventListener('click', () => {
if(pauseTime) {
pauseTime = null;
} else {
if(start) return;
start = new Date();
intervalId = setInterval(startTimer, 1000);
}
document.cookie = "timer=" + String(start.getTime());
})
document.getElementById('stop-btn').addEventListener('click', () => {
pauseTime = Date.now() - start.getTime();
document.cookie = "timer=" + String(-pauseTime);
});
document.getElementById('reset-btn').addEventListener('click', () => {
start = null;
document.cookie = "timer=null";
if(intervalId) clearInterval(intervalId);
document.getElementById("hour").innerHTML = '0';
document.getElementById("minute").innerHTML = '0';
document.getElementById("seconds").innerHTML = '0';
});
</script>
</div>
</body>
</html>
不要每秒钟增加一次计时器,而是用计时器启动的时间减去当前时间
<!DOCTYPE html>
<html>
<head>
<title>Timer test</title>
</head>
<body>
<div id="time">
<div><span id="hour"></span>:<span id="minute"></span>:<span id="seconds"></span></div>
<button id="start-btn">Start</button>
<button id="stop-btn">Pause</button>
<button id="reset-btn">Reset</button>
</div>
<script type="text/javascript">
let hour = 0;
let minute = 0;
let seconds = 0;
let pauseTime = null;
let start = [...document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x => x[1].trim() == 'timer').map(x => Number(x[2].trim()))[0];
if(start != null) {
if(start > 0) start = new Date(start);
else if(start < 0) {
pauseTime = -start;
start = new Date(Date.now() + start); //+- = -
} else start = null;
} else start = null;
let intervalId = null;
function startTimer() {
let totalSeconds;
if(pauseTime) {
start = new Date(Date.now() - pauseTime);
totalSeconds = pauseTime;
return;
} else {
totalSeconds = Math.floor((Date.now() - start.getTime()) / 1000);
}
hour = Math.floor(totalSeconds /3600);
minute = Math.floor((totalSeconds - hour*3600)/60);
seconds = totalSeconds - (hour*3600 + minute*60);
document.getElementById("hour").innerHTML =hour;
document.getElementById("minute").innerHTML =minute;
document.getElementById("seconds").innerHTML =seconds;
}
if(start) intervalId = setInterval(startTimer, 1000);
document.getElementById('start-btn').addEventListener('click', () => {
if(pauseTime) {
pauseTime = null;
} else {
if(start) return;
start = new Date();
intervalId = setInterval(startTimer, 1000);
}
document.cookie = "timer=" + String(start.getTime());
})
document.getElementById('stop-btn').addEventListener('click', () => {
pauseTime = Date.now() - start.getTime();
document.cookie = "timer=" + String(-pauseTime);
});
document.getElementById('reset-btn').addEventListener('click', () => {
start = null;
document.cookie = "timer=null";
if(intervalId) clearInterval(intervalId);
document.getElementById("hour").innerHTML = '0';
document.getElementById("minute").innerHTML = '0';
document.getElementById("seconds").innerHTML = '0';
});
</script>
</div>
</body>
</html>
计时器测试
::
开始
暂停
重置
设小时=0;
设分钟=0;
设秒=0;
让pauseTime=null;
let start=[…document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x=>x[1]。trim()==“timer”).map(x=>Number(x[2]。trim())[0];
如果(开始!=null){
如果(开始>0)开始=新日期(开始);
如果(开始<0){
pauseTime=-start;
开始=新日期(Date.now()+开始);//+-=-
}else start=null;
}else start=null;
设intervalId=null;
函数startTimer(){
让我们等待几秒钟;
如果(暂停时间){
开始=新日期(Date.now()-pauseTime);
totalSeconds=暂停时间;
返回;
}否则{
totalSeconds=Math.floor((Date.now()-start.getTime())/1000);
}
小时=数学楼层(总秒数/3600);
分钟=数学地板((总秒-小时*3600)/60);
秒=总秒-(小时*3600+分钟*60);
document.getElementById(“hour”).innerHTML=hour;
document.getElementById(“分钟”).innerHTML=minute;
document.getElementById(“秒”).innerHTML=seconds;
}
如果(开始)有效期=设置间隔(起始时间,1000);
document.getElementById('start-btn')。addEventListener('click',()=>{
如果(暂停时间){
pauseTime=null;
}否则{
如果(启动)返回;
开始=新日期();
intervalId=setInterval(起始时间,1000);
}
document.cookie=“timer=”+字符串(start.getTime());
})
document.getElementById('stop-btn')。addEventListener('click',()=>{
pauseTime=Date.now()-start.getTime();
document.cookie=“timer=”+字符串(-pauseTime);
});
document.getElementById('reset-btn')。addEventListener('click',()=>{
start=null;
document.cookie=“timer=null”;
if(有效期)clearInterval(有效期);
document.getElementById(“hour”).innerHTML='0';
document.getElementById(“分钟”).innerHTML='0';
document.getElementById(“秒”).innerHTML='0';
});
如果要防止计时器重置,请使用cookie存储计时器启动的日期
<!DOCTYPE html>
<html>
<head>
<title>Timer test</title>
</head>
<body>
<div id="time">
<div><span id="hour"></span>:<span id="minute"></span>:<span id="seconds"></span></div>
<button id="start-btn">Start</button>
<button id="stop-btn">Pause</button>
<button id="reset-btn">Reset</button>
</div>
<script type="text/javascript">
let hour = 0;
let minute = 0;
let seconds = 0;
let pauseTime = null;
let start = [...document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x => x[1].trim() == 'timer').map(x => Number(x[2].trim()))[0];
if(start != null) {
if(start > 0) start = new Date(start);
else if(start < 0) {
pauseTime = -start;
start = new Date(Date.now() + start); //+- = -
} else start = null;
} else start = null;
let intervalId = null;
function startTimer() {
let totalSeconds;
if(pauseTime) {
start = new Date(Date.now() - pauseTime);
totalSeconds = pauseTime;
return;
} else {
totalSeconds = Math.floor((Date.now() - start.getTime()) / 1000);
}
hour = Math.floor(totalSeconds /3600);
minute = Math.floor((totalSeconds - hour*3600)/60);
seconds = totalSeconds - (hour*3600 + minute*60);
document.getElementById("hour").innerHTML =hour;
document.getElementById("minute").innerHTML =minute;
document.getElementById("seconds").innerHTML =seconds;
}
if(start) intervalId = setInterval(startTimer, 1000);
document.getElementById('start-btn').addEventListener('click', () => {
if(pauseTime) {
pauseTime = null;
} else {
if(start) return;
start = new Date();
intervalId = setInterval(startTimer, 1000);
}
document.cookie = "timer=" + String(start.getTime());
})
document.getElementById('stop-btn').addEventListener('click', () => {
pauseTime = Date.now() - start.getTime();
document.cookie = "timer=" + String(-pauseTime);
});
document.getElementById('reset-btn').addEventListener('click', () => {
start = null;
document.cookie = "timer=null";
if(intervalId) clearInterval(intervalId);
document.getElementById("hour").innerHTML = '0';
document.getElementById("minute").innerHTML = '0';
document.getElementById("seconds").innerHTML = '0';
});
</script>
</div>
</body>
</html>
不要每秒钟增加一次计时器,而是用计时器启动的时间减去当前时间
<!DOCTYPE html>
<html>
<head>
<title>Timer test</title>
</head>
<body>
<div id="time">
<div><span id="hour"></span>:<span id="minute"></span>:<span id="seconds"></span></div>
<button id="start-btn">Start</button>
<button id="stop-btn">Pause</button>
<button id="reset-btn">Reset</button>
</div>
<script type="text/javascript">
let hour = 0;
let minute = 0;
let seconds = 0;
let pauseTime = null;
let start = [...document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x => x[1].trim() == 'timer').map(x => Number(x[2].trim()))[0];
if(start != null) {
if(start > 0) start = new Date(start);
else if(start < 0) {
pauseTime = -start;
start = new Date(Date.now() + start); //+- = -
} else start = null;
} else start = null;
let intervalId = null;
function startTimer() {
let totalSeconds;
if(pauseTime) {
start = new Date(Date.now() - pauseTime);
totalSeconds = pauseTime;
return;
} else {
totalSeconds = Math.floor((Date.now() - start.getTime()) / 1000);
}
hour = Math.floor(totalSeconds /3600);
minute = Math.floor((totalSeconds - hour*3600)/60);
seconds = totalSeconds - (hour*3600 + minute*60);
document.getElementById("hour").innerHTML =hour;
document.getElementById("minute").innerHTML =minute;
document.getElementById("seconds").innerHTML =seconds;
}
if(start) intervalId = setInterval(startTimer, 1000);
document.getElementById('start-btn').addEventListener('click', () => {
if(pauseTime) {
pauseTime = null;
} else {
if(start) return;
start = new Date();
intervalId = setInterval(startTimer, 1000);
}
document.cookie = "timer=" + String(start.getTime());
})
document.getElementById('stop-btn').addEventListener('click', () => {
pauseTime = Date.now() - start.getTime();
document.cookie = "timer=" + String(-pauseTime);
});
document.getElementById('reset-btn').addEventListener('click', () => {
start = null;
document.cookie = "timer=null";
if(intervalId) clearInterval(intervalId);
document.getElementById("hour").innerHTML = '0';
document.getElementById("minute").innerHTML = '0';
document.getElementById("seconds").innerHTML = '0';
});
</script>
</div>
</body>
</html>
计时器测试
::
开始
暂停
重置
设小时=0;
设分钟=0;
设秒=0;
让pauseTime=null;
let start=[…document.cookie.matchAll(/([^;=]+)=([^;=]+)(;|$)/g)].filter(x=>x[1]。trim()==“timer”).map(x=>Number(x[2]。trim())[0];
如果(开始!=null){
如果(开始>0)开始=新日期(开始);
如果(开始<0){
pauseTime=-start;
开始=新日期(Date.now()+开始);//+-=-
}else start=null;
}else start=null;
设intervalId=null;
函数startTimer(){
让我们等待几秒钟;
如果(暂停时间){
开始=新日期(Date.now()-pauseTime);
totalSeconds=暂停时间;
返回;
}否则{
totalSeconds=Math.floor((Date.now()-start.getTime())/1000);
}
小时=数学楼层(总秒数/3600);
分钟=数学地板((总秒-小时*3600)/60);
秒=总秒-(小时*3600+分钟*60);
document.getElementById(“hour”).innerHTML=hour;
document.getElementById(“分钟”).innerHTML=minute;
document.getElementById(“秒”).innerHTML=seconds;
}
如果(开始)有效期=设置间隔(起始时间,1000);
document.getElementById('start-btn')。addEventListener('click',()=>{
如果(暂停时间){
pauseTime=null;
}否则{
如果(启动)返回;
开始=新日期();
intervalId=setInter