尊敬来自世界各地的专业人士。我有一个初学者的问题。我没有找到任何直接的答案,只是一些参考..。我编写了一个简单版本的秒表(React),当我尝试在不同的浏览器(Chrome、Mozilla和Edge)上测试它时,我在这个秒表上的Chrome(Edge)和Mozilla之间有不同的速度结果。差别约为4倍。如果你还能给我发一个链接给我一些理论(也许),我将非常感激。
不管怎样,谢谢
如果您在这段代码中看到一些奇怪的东西,请告诉我。那对我很有用。
import React, { useState } from "react";
import { useEffect } from "react";
import "./Stopwatch.css";
import Button from "./Button";
import Round from "./Round";
function Stopwach() {
const [timer, setTimer] = useState(0);
const [start, setStart] = useState(false);
const [round, setRound] = useState([]);
const [globeTimer, setGlobeTimer] = useState(0);
useEffect(() => {
let secondsCircle = document.querySelector(".svg01");
let milCircle = document.querySelector(".svg02");
let degreeS = 0;
let degreeM = 0;
degreeS = timer / 166.6666666666667;
secondsCircle.style.transform = `rotate(${degreeS}deg)`;
degreeM = timer / 3.6;
milCircle.style.transform = `rotate(${degreeM}deg)`;
}, [timer]);
function Circle() {
if (round.length < 10) {
if (round.length === 0) {
round[round.length] = timer;
setRound(round);
} else {
round[round.length] = timer - globeTimer;
setRound(round);
}
} else {
let firstElement = Math.min.apply(Math, round);
round.length = 0;
round[0] = firstElement;
round[round.length] = timer - globeTimer;
setRound(round);
}
setGlobeTimer(timer);
}
useEffect(() => {
let interval;
if (start) {
interval = setInterval(() => {
setTimer((timer) => timer + 4);
}, 1);
} else {
clearInterval(interval);
}
return () => {
clearInterval(interval);
};
}, [start]);
function go() {
setStart(!start);
}
function clear() {
setTimer(0);
setRound([]);
setGlobeTimer([])
}
return (
<div className="main_monitor">
<div className="svg">
<div className="svg01"></div>
<div className="svg02"></div>
</div>
<div className="main_monitor__timer">
<div className="mill">
{Math.trunc(timer / 1000 / 60) < 10 ? `0${Math.trunc(timer / 1000 / 60)}` : Math.trunc(timer / 1000 / 60)}
</div>
<div className="point">:</div>
<div className="mill">
{Math.trunc(timer / 1000) % 60 < 10 ? `0${Math.trunc(timer / 1000) % 60}` : Math.trunc(timer / 1000) % 60}
</div>
<div className="point">.</div>
<div className="mill">{timer % 1000 < 10 ? `00${timer % 1000}` : timer % 1000 < 100 ? `0${timer % 1000}` : timer % 1000} </div>
</div>
<div className="main_monitor__btns">
<Button go={go} btn={"play"} />
<Button go={go} btn={"stop"} />
<Button go={clear} btn={"recycle"} />
<Button go={Circle} btn={"history"} />
</div>
<Round rounds={round} />
</div>
);
}
export default Stopwach;发布于 2022-07-12 15:27:40
所以,在喝了5天的咖啡后.根据Wiki - Firefox的说法,Google和Edge是具有不同引擎的浏览器。我认为,至少这是这个错误的根源。
我用Date()构造函数解决了这个问题。每次我们调用setInterval时,我们都会问新的“一个独立于平台的格式的时刻”(不幸的是,这部分定义没有在MDN上翻译(en - ru) )。基于这个“单一时刻”,我们可以计算出从我们的时间点过去的时间.
https://stackoverflow.com/questions/72880755
复制相似问题