Javascript 计时器仅用于分秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19429890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript timer just for minutes and seconds
提问by mike
回答by Rayon
DATE_OBJ.getSeconds()
to get seconds ofDate
object.DATE_OBJ. getMinutes()
to get minutes ofDate
object.setInterval
to invoke handler function after every second(1000ms
).
DATE_OBJ.getSeconds()
获取Date
对象的秒数。DATE_OBJ. getMinutes()
获取分钟的Date
对象。setInterval
每秒调用一次处理函数(1000ms
)。
var handler = function() {
var date = new Date();
var sec = date.getSeconds();
var min = date.getMinutes();
document.getElementById("time").textContent = (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec);
};
setInterval(handler, 1000);
handler();
<h1 id="time" style="text-align: center"></h1>
回答by Vandesh
Here's a very hackish approach - http://jsfiddle.net/gPrwW/1/
这是一个非常hackish的方法 - http://jsfiddle.net/gPrwW/1/
HTML-
HTML-
<div id="worked">31:14</div>
JS :
JS:
$(document).ready(function (e) {
var $worked = $("#worked");
function update() {
var myTime = $worked.html();
var ss = myTime.split(":");
var dt = new Date();
dt.setHours(0);
dt.setMinutes(ss[0]);
dt.setSeconds(ss[1]);
var dt2 = new Date(dt.valueOf() + 1000);
var temp = dt2.toTimeString().split(" ");
var ts = temp[0].split(":");
$worked.html(ts[1]+":"+ts[2]);
setTimeout(update, 1000);
}
setTimeout(update, 1000);
});
回答by Tibos
The precise way of handling this is the following:
处理这种情况的确切方法如下:
- store the time of the start of the script
- in a function that gets called repeatedly get the time elapsed
- convert the elapsed time in whatever format you want and show it
- 存储脚本开始的时间
- 在被重复调用的函数中获取经过的时间
- 以您想要的任何格式转换经过的时间并显示它
Sample code:
示例代码:
var initialTime = Date.now();
function checkTime(){
var timeDifference = Date.now() - initialTime;
var formatted = convertTime(timeDifference);
document.getElementById('time').innerHTML = '' + formatted;
}
function convertTime(miliseconds) {
var totalSeconds = Math.floor(miliseconds/1000);
var minutes = Math.floor(totalSeconds/60);
var seconds = totalSeconds - minutes * 60;
return minutes + ':' + seconds;
}
window.setInterval(checkTime, 100);
You can easily change the granularity of checking the time (currently set at 0.1 seconds). This timer has the advantage that it will never be out of sync when it updates.
您可以轻松更改检查时间的粒度(目前设置为 0.1 秒)。这个计时器的优点是它在更新时永远不会不同步。
回答by Emre
You can make a function that increments a counter every time it's called, shows the value as: counter/60 minutes, counter%60 seconds
您可以创建一个每次调用都会增加计数器的函数,将值显示为:counter/60 分钟,counter%60 秒
Then you can use the setInterval
function to make JavaScript call your code every second. It's not extremely precise, but it's good enough for simple timers.
然后您可以使用该setInterval
函数让 JavaScript 每秒调用您的代码。它不是非常精确,但对于简单的计时器来说已经足够了。
回答by user8000103
var initialTime = Date.now();
function checkTime(){
var timeDifference = Date.now() - initialTime;
var formatted = convertTime(timeDifference);
document.getElementById('time').innerHTML = '' + formatted;
}
function convertTime(miliseconds) {
var totalSeconds = Math.floor(miliseconds/1000);
var minutes = Math.floor(totalSeconds/60);
var seconds = totalSeconds - minutes * 60;
return minutes + ':' + seconds;
}
window.setInterval(checkTime, 100);
回答by Hiny
This might be something? plain count up timer in javascript
这可能是什么? javascript中的简单计数计时器
It is based on the setInterval method
它基于 setInterval 方法
setInterval(setTime, 1000);