在 JavaScript 中从头开始创建秒表
Creating a stopwatch from scratch in JavaScript
通过为毫秒、秒和分钟创建变量,我想做一个小秒表项目。我的按钮 onClick
是 theTimer()
:
var milli = 0;
var seconds = 0;
var minutes = 0;
var onOff = 0;
var txtMilli = document.getElementById("txtMilli");
var txtSeconds = document.getElementById("txtSeconds");
var txtMinutes = document.getElementById("txtMinutes");
function theTimer()
{
if (onOff == 0) {
onOff = 1;
timer = setInterval("startCounting()",1);
}
if (onOff == 1) {
onOff == 0;
clearInterval(timer);
}
}
function startCounting()
{
if (milli >999)
{
milli = 0; if (seconds <60) {seconds +=1}
}
else
{
milli +=1;
}
if (seconds >59)
{
seconds = 0; minutes += 1;
}
if (milli > 10)
{
txtMilli.innerHTML = "0" + milli;
}
if (milli < 10)
{
txtMilli.innerHTML = "" + milli;
}
}
控制台中没有显示任何内容,问题是当 运行 时 DIV
s 永远不会从 0 改变。
我像这样修复了你的代码:
var milli = 0;
var seconds = 0;
var minutes = 0;
var onOff = 0;
var txtMilli = document.getElementById("txtMilli");
var txtSeconds = document.getElementById("txtSeconds");
var txtMinutes = document.getElementById("txtMinutes");
function startCounting(){
if (milli >999) { milli = 0; if (seconds <60) {seconds +=1} }
else {milli +=1;}
if (seconds >59) {seconds = 0; minutes += 1;}
if (milli > 10) {txtMilli.innerHTML = "0" + milli;}
if (milli < 10) {txtMilli.innerHTML = "" + milli;}
}
function theTimer(){
if (onOff == 0) {
onOff = 1;
timer = setInterval(startCounting, 1);
} else if (onOff == 1) {
onOff = 0;
clearInterval(timer);
}
}
myButton.onclick = theTimer;
theTimer
中存在错误,您将 onOff
设置为 1
,然后立即检查它是否为 1
并将其设置回0
。我使用 else if
解决了这个问题。
我还把onOff == 0
(比较)改成了onOff = 0
(赋值)。
工作 JSFiddle:https://jsfiddle.net/k83tvfhc/
通过为毫秒、秒和分钟创建变量,我想做一个小秒表项目。我的按钮 onClick
是 theTimer()
:
var milli = 0;
var seconds = 0;
var minutes = 0;
var onOff = 0;
var txtMilli = document.getElementById("txtMilli");
var txtSeconds = document.getElementById("txtSeconds");
var txtMinutes = document.getElementById("txtMinutes");
function theTimer()
{
if (onOff == 0) {
onOff = 1;
timer = setInterval("startCounting()",1);
}
if (onOff == 1) {
onOff == 0;
clearInterval(timer);
}
}
function startCounting()
{
if (milli >999)
{
milli = 0; if (seconds <60) {seconds +=1}
}
else
{
milli +=1;
}
if (seconds >59)
{
seconds = 0; minutes += 1;
}
if (milli > 10)
{
txtMilli.innerHTML = "0" + milli;
}
if (milli < 10)
{
txtMilli.innerHTML = "" + milli;
}
}
控制台中没有显示任何内容,问题是当 运行 时 DIV
s 永远不会从 0 改变。
我像这样修复了你的代码:
var milli = 0;
var seconds = 0;
var minutes = 0;
var onOff = 0;
var txtMilli = document.getElementById("txtMilli");
var txtSeconds = document.getElementById("txtSeconds");
var txtMinutes = document.getElementById("txtMinutes");
function startCounting(){
if (milli >999) { milli = 0; if (seconds <60) {seconds +=1} }
else {milli +=1;}
if (seconds >59) {seconds = 0; minutes += 1;}
if (milli > 10) {txtMilli.innerHTML = "0" + milli;}
if (milli < 10) {txtMilli.innerHTML = "" + milli;}
}
function theTimer(){
if (onOff == 0) {
onOff = 1;
timer = setInterval(startCounting, 1);
} else if (onOff == 1) {
onOff = 0;
clearInterval(timer);
}
}
myButton.onclick = theTimer;
theTimer
中存在错误,您将 onOff
设置为 1
,然后立即检查它是否为 1
并将其设置回0
。我使用 else if
解决了这个问题。
我还把onOff == 0
(比较)改成了onOff = 0
(赋值)。
工作 JSFiddle:https://jsfiddle.net/k83tvfhc/