gpt4 book ai didi

以毫秒为增量的 Javascript 秒表

转载 作者:行者123 更新时间:2023-11-30 14:19:24 25 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 制作一个秒表。我有日期来填充正确的信息,但我的秒表不工作。我单击开始,数字永远不会从 0 移动,我想让它在 MS 中立即递增。我也有 JS 和 HTML 的代码。 HTML 正常运行,但 JS 却没有。我对 JavaScript 世界非常陌生,我看了又看,但无法找到对我有益的解决方案。感谢你的协助。

"use strict";
var $ = function(id) { return document.getElementById(id); };

var stopwatchTimer;

var elapsedMinutes = 0;

var elapsedSeconds = 0;

var elapsedMilliseconds = 0;

var displayCurrentTime = function() {

var now = new Date();

var hours = now.getHours();

var ampm = "AM";
if (hours > 12) {

hours = hours - 12;

ampm = "PM";


} else {

switch (hours) {

case 12:

ampm = "PM";

break;

case 0:

hours = 12;

ampm = "AM";

}

}


$("hours").firstChild.nodeValue = hours;

$("minutes").firstChild.nodeValue = padSingleDigit(now.getMinutes());

$("seconds").firstChild.nodeValue = padSingleDigit(now.getSeconds());

$("ampm").firstChild.nodeValue = ampm;

};

var padSingleDigit = function(num) {

if (num < 10) { return "0" + num; }

else { return num; }

};

var tickStopwatch = function() {
// I also need to increment in 10 milliseconds increments but unsure if I //have this right


var ms=0;
var sec=0;
var min=0;

var frame= function() {
If(ms==1000)
ms=0;
sec++;
}
if(sec==60) {
sec=0;
min++;

document.getElementById("s_seconds").innerHTML = valueOf(sec);
document.getElementById("s_minutes").innerHTML = valueOf(min);
document.getElementById("s_ms").innerHTML = valueOf(ms);
}

};

var startStopwatch = function(evt) {

};

var stopStopwatch = function(evt) {

};

var resetStopwatch = function(evt) {

};
window.onload = function() {

displayCurrentTime();
setInterval(tickStopwatch, 1000);
};
"use strict"; //evt is in a separate file
var evt = {
attach: function(node, eventName, func) {

},
detach: function(node, eventName, func) {

},
preventDefault: function(e) {

}
};


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Clock</title>
<link rel="stylesheet" href="clock.css">

<script src="library_event.js"></script>

<script src="clock.js"></script>

</head>
<body>
<main>
<h1>Digital clock with stopwatch</h1>
<fieldset>
<legend>Clock</legend>
<span id="hours">&nbsp;</span>:
<span id="minutes">&nbsp;</span>:
<span id="seconds">&nbsp;</span>&nbsp;
<span id="ampm">&nbsp;</span>
</fieldset>

<fieldset>
<legend>Stop Watch</legend>
<a href="#" id="start">Start</a>&nbsp;

<a href="#" id="stop">Stop</a>&nbsp;

<a href="#" id="reset">Reset</a>&nbsp;

<span id="s_minutes">00</span>:
<span id="s_seconds">00</span>:
<span id="s_ms">000</span>
</fieldset>
</main>
</body>
</html>

最佳答案

如果您增量更新值 mssecondsminutes,您将永远不会准确。每次更新都会浪费时间。在 JS 中根本没有可以以那种速度和精度运行的区间。

相反,从内部时钟计算它们

let offset = 0,
paused = true;

render();

function startStopwatch(evt) {
if (paused) {
paused = false;
offset -= Date.now();
render();
}
}

function stopStopwatch(evt) {
if (!paused) {
paused = true;
offset += Date.now();
}
}

function resetStopwatch(evt) {
if (paused) {
offset = 0;
render();
} else {
offset = -Date.now();
}
}

function format(value, scale, modulo, padding) {
value = Math.floor(value / scale) % modulo;
return value.toString().padStart(padding, 0);
}

function render() {
var value = paused ? offset : Date.now() + offset;

document.querySelector('#s_ms').textContent = format(value, 1, 1000, 3);
document.querySelector('#s_seconds').textContent = format(value, 1000, 60, 2);
document.querySelector('#s_minutes').textContent = format(value, 60000, 60, 2);

if(!paused) {
requestAnimationFrame(render);
}
}
<fieldset>
<legend>Stop Watch</legend>
<a href="#" id="start" onclick="startStopwatch()">Start</a>&nbsp;

<a href="#" id="stop" onclick="stopStopwatch()">Stop</a>&nbsp;

<a href="#" id="reset" onclick="resetStopwatch()">Reset</a>&nbsp;

<span id="s_minutes">00</span>:
<span id="s_seconds">00</span>:
<span id="s_ms">000</span>
</fieldset>

offset 存储两个值:当 paused 时,它存储您停止时的值,否则它存储您必须添加到 Date.now( ) 计算值。

该值是以毫秒为单位的时间,计算秒和分钟是基本算术。

关于以毫秒为增量的 Javascript 秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com