秒表/计时器,保存var并显示在高分列表(HTML, JS)
Stopwatch / Timer, save var and show in high score list (HTML, JS)
我正在构建一个需要计时器的web应用程序:在特定事件时启动,停止和重置。之后,我想将变量保存在类似"hilicore"的列表中。
该应用程序应该与AR眼镜一起用于维护工作。
应用程序应该包含测量作业持续时间的功能。
这是与我的问题相关的代码:
JS:
var clsStopwatch = function() {
// Private vars
var startAt = 0; // Time of last start / resume. (0 if not running)
var lapTime = 0; // Time on the clock when last stopped in milliseconds
var now = function() {
return (new Date()).getTime();
};
// Public methods
// Start or resume
this.start = function() {
startAt = startAt ? startAt : now();
};
// Stop or pause
this.stop = function() {
// If running, update elapsed time otherwise keep it
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0; // Paused
};
// Reset
this.reset = function() {
lapTime = startAt = 0;
};
// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
var ourTime = 3;
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor( time / (60 * 60 * 1000) );
time = time % (60 * 60 * 1000);
m = Math.floor( time / (60 * 1000) );
time = time % (60 * 1000);
s = Math.floor( time / 1000 );
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}
function show() {
$time = document.getElementById('time');
update();
}
function update() {
$time.innerHTML = formatTime(x.time());
}
function start() {
clocktimer = setInterval("update()", 1);
x.start();
}
function stop() {
x.stop();
clearInterval(clocktimer);
}
function reset() {
stop();
x.reset();
update();
}
HTML: <head>
<meta charset="UTF-8">
<title>Stopwatch</title>
</head>
<body onload="show();">
<div>Time: <span id="time"></span></div>
<input type="button" value="start" onclick="start();">
<input type="button" value="stop" onclick="stop();">
<input type="button" value="reset" onclick="reset()">
</body>
<div id="result"></div>
<script>
if (typeof(Storage) != "undefined") {
// Store
localStorage.setItem("lastname", ourTime);
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>
在这个例子中,值3
被保存并正确显示,但是我不能显示秒表停止后的时间。
我想到的最好的是:
var ourTime = formatTime(X)
这将以正确的格式显示数字,但值始终是00:00:00或未定义。我如何得到正确的秒表停止值?
问题是您试图更新未定义的字段;update()中调用的$time值被设置为NULL。为了解决这个问题,我在start()中声明了$time,现在它似乎工作了。此外,您根本没有使用show()函数,所以我只是把它遗漏了,就像localStorage一样,因为它对问题没有贡献,xy没有定义,因此它只是为我们创建了一个错误。
var clsStopwatch = function() {
// Private vars
var startAt = 0; // Time of last start / resume. (0 if not running)
var lapTime = 0; // Time on the clock when last stopped in milliseconds
var now = function() {
return (new Date()).getTime();
};
// Public methods
// Start or resume
this.start = function() {
startAt = startAt ? startAt : now();
};
// Stop or pause
this.stop = function() {
// If running, update elapsed time otherwise keep it
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0; // Paused
};
// Reset
this.reset = function() {
lapTime = startAt = 0;
};
// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
var ourTime = 3;
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor(time / (60 * 60 * 1000));
time = time % (60 * 60 * 1000);
m = Math.floor(time / (60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time / 1000);
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}
function update() {
$time.innerHTML = formatTime(x.time());
}
function start() {
//Declare $time here, so update() knows which field it needs to update
$time = document.getElementById('time');
clocktimer = setInterval("update()", 41);
x.start();
}
function stop() {
x.stop();
clearInterval(clocktimer);
}
function reset() {
stop();
x.reset();
update();
}
<div>Time: <span id="time"></span></div>
<input type="button" value="start" onclick="start();">
<input type="button" value="stop" onclick="stop();">
<input type="button" value="reset" onclick="reset()">
相关文章:
- 如何在HTML列表中添加事件's子弹
- 从JSON填充列表(而不是HTML)
- 为非列表项目创建HTML实时搜索
- 如何使用jQuery来解析json并将其作为列表输出到HTML中
- html5中数据描述属性中的html列表
- javascript和html图像下拉列表
- 我在下拉列表中尝试了下一个和前五年的html代码.接下来的5年我都过得很好.我怎样才能拿到之前的5年
- 通过点击-PHP/HTML发布列表中的选定值
- 用javascript在html显示列表中显示新成员
- 如何在列表(HTML)中创建文本框
- 在选择其他下拉列表html和javascript araray的基础上,填充3个连续的下拉列表
- 使用 SQL 信息更新选择下拉列表 (HTML)
- 从文本区域中的选定文本创建列表html
- 层次结构和多选项下拉列表(HTML),并在项目之间导航(子节点)
- 显示选项与复选框下拉列表- HTML/JAVASCRIPT
- 渐近分析:填充一个长重复列表.HTML vs. JavaScript
- 秒表/计时器,保存var并显示在高分列表(HTML, JS)
- Chrome中的AJAX下拉列表(HTML Select)
- 转换无序列表html
- 对json数组递归调用子列表
- 如何使用jQuery在表格或列表html之间切换