在我的项目中,我使用React Countdown Clock Timer NPM包。我有两个计时器同时出现,一个是分钟计时器,另一个是秒计时器。一切都很好,除了当我切换选项卡时,我的分钟数保持正确的分钟数,而我的秒计时器重置。我希望秒计时器即使在切换选项卡时也能保持正确的设置量。
这是我当前的计时器文件,其中iies
import React, { useState, useContext } from "react";
import { CountdownCircleTimer } from "react-countdown-circle-timer";
import { AuthContext } from "../../context/Auth";
import { ChillContext } from "../../context/ChillContext";
import { db } from "../../services/firebase";
import firebase from "firebase/app";
import chime from "../../sounds/chime.mp3";
import {
CountdownTimerWrapper,
CountdownTimerMain,
TimersWrapper,
RestartButton,
StartStopButton,
ButtonsWrapper,
TimerCheck,
TimerHeading,
TimerControl,
TimerText,
} from "./CountdownTimerElements";
import Toggle from "../Toggle/Toggle";
import { Howl } from "howler";
const minuteSeconds = 60;
const hourSeconds = 3600;
const renderTime = (dimension, time) => {
return (
<div className="time-wrapper">
<div className="time">{time}</div>
<div>{dimension}</div>
</div>
);
};
const getTimeSeconds = (time) => (minuteSeconds - time) | 0;
const getTimeMinutes = (time) => ((time % hourSeconds) / minuteSeconds) | 0;
const CountdownTimer = () => {
const stratTime = Date.now() / 1000; // use UNIX timestamp in seconds
const workEndTime = stratTime + 25 * 60; // use UNIX timestamp in seconds
const chillEndTime = stratTime + 5 * 60;
const [minutesKey, setMinutesKey] = useState(0);
const [secondsKey, setSecondsKey] = useState(10);
const [playing, setPlaying] = useState(false);
const [timerButtonDesc, setTimerButtonDesc] = useState("Start");
const { currentUser } = useContext(AuthContext);
const { chill, setChill } = useContext(ChillContext);
const chillRemainingTime = chillEndTime - stratTime;
const workRemainingTime = workEndTime - stratTime;
const timerProps = {
isPlaying: playing,
size: 100,
strokeWidth: 6,
};
const sound = new Howl({
src: [chime],
});
return (
<CountdownTimerWrapper>
<CountdownTimerMain>
<TimerHeading>Pomodoro Timer</TimerHeading>
{chill ? (
<TimersWrapper>
<CountdownCircleTimer
{...timerProps}
colors={[["#9294e3"]]}
key={minutesKey}
duration={hourSeconds}
initialRemainingTime={chillRemainingTime % hourSeconds}
onComplete={(totalElapsedTime) => {
return [chillRemainingTime - totalElapsedTime > minuteSeconds];
}}
>
{({ elapsedTime }) =>
renderTime("minutes", getTimeMinutes(hourSeconds - elapsedTime))
}
</CountdownCircleTimer>
<TimerControl>
<Toggle
onChange={(event) => {
setChill(event.target.checked);
setSecondsKey((prevKey) => prevKey + 1);
setMinutesKey((prevKey) => prevKey + 1);
setTimerButtonDesc("Start");
setPlaying(false);
}}
/>
<TimerText>{chill ? "Chill" : "Work"} Timer </TimerText>
</TimerControl>
<CountdownCircleTimer
{...timerProps}
colors={[["#9294e3"]]}
key={secondsKey}
duration={minuteSeconds}
initialRemainingTime={chillRemainingTime % minuteSeconds}
onComplete={(totalElapsedTime) => {
console.log(totalElapsedTime - chillRemainingTime);
if (totalElapsedTime - chillRemainingTime === 0) {
return false;
}
return [chillRemainingTime - totalElapsedTime > 0];
}}
>
{({ elapsedTime }) =>
renderTime("seconds", getTimeSeconds(elapsedTime))
}
</CountdownCircleTimer>
</TimersWrapper>
) : (
<TimersWrapper>
<CountdownCircleTimer
{...timerProps}
colors={[["#d87463"]]}
key={minutesKey}
duration={hourSeconds}
initialRemainingTime={workRemainingTime % hourSeconds}
onComplete={(totalElapsedTime) => {
return [workRemainingTime - totalElapsedTime > minuteSeconds];
}}
>
{({ elapsedTime }) =>
renderTime("minutes", getTimeMinutes(hourSeconds - elapsedTime))
}
</CountdownCircleTimer>
<TimerControl>
<Toggle
onChange={(event) => {
setChill(event.target.checked);
setSecondsKey((prevKey) => prevKey + 1);
setMinutesKey((prevKey) => prevKey + 1);
setTimerButtonDesc("Start");
setPlaying(false);
}}
/>
<TimerText>{chill ? "Chill" : "Work"} Timer </TimerText>
</TimerControl>
<CountdownCircleTimer
{...timerProps}
colors={[["#d87463"]]}
key={secondsKey}
duration={minuteSeconds}
initialRemainingTime={workRemainingTime % minuteSeconds}
onComplete={(totalElapsedTime) => {
if (totalElapsedTime - workRemainingTime === 0) {
db.collection("emails")
.doc(currentUser.email)
.update({
pomodoros: firebase.firestore.FieldValue.increment(1),
});
sound.play();
}
return [workRemainingTime - totalElapsedTime > 0];
}}
>
{({ elapsedTime }) =>
renderTime("seconds", getTimeSeconds(elapsedTime))
}
</CountdownCircleTimer>
</TimersWrapper>
)}
<ButtonsWrapper>
<RestartButton
onClick={() => {
setSecondsKey((prevKey) => prevKey + 1);
setMinutesKey((prevKey) => prevKey + 1);
setPlaying(false);
setTimerButtonDesc("Start");
}}
>
Restart Timer
</RestartButton>
<TimerCheck />
<StartStopButton
onClick={() => {
if (timerButtonDesc === "Start") {
setTimerButtonDesc("Stop");
} else {
setTimerButtonDesc("Start");
}
setPlaying(!playing);
}}
>
{timerButtonDesc}
</StartStopButton>
</ButtonsWrapper>
</CountdownTimerMain>
</CountdownTimerWrapper>
);
};
export default CountdownTimer;
您遇到的问题是您正在设置"键";到minutesKey。每次你改变";键";计时器将重置。如果重置不是folw的一部分,则需要传入一个常量。如果重置是流程的一部分,则有一个单独的状态。
<CountdownCircleTimer
{...timerProps}
colors={[["#d87463"]]}
key={0}
duration={minuteSeconds}
initialRemainingTime={workRemainingTime % minuteSeconds}
onComplete={(totalElapsedTime) => {
if (totalElapsedTime - workRemainingTime === 0) {
db.collection("emails")
.doc(currentUser.email)
.update({
pomodoros: firebase.firestore.FieldValue.increment(1),
});
sound.play();
}
return [workRemainingTime - totalElapsedTime > 0];
}}
>
{({ elapsedTime }) =>
renderTime("seconds", getTimeSeconds(elapsedTime))
}
</CountdownCircleTimer>