将时钟(日期和时间)运行速度提高四倍的 JavaScript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14272133/
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 code to run the Clock (date and time) four times faster
提问by logan
I am in need of virtual time (4 x current date and time). I have managed to display the running clock with the current date and time, but I am unable to the time four times faster than current time.
我需要虚拟时间(4 x 当前日期和时间)。我已经设法显示当前日期和时间的运行时钟,但我无法比当前时间快四倍。
For example, if the current date is 01-01-2012 00:00:00, the virtual time should be 01-01-2012 00:00:04
例如,如果当前日期为 01-01-2012 00:00:00,则虚拟时间应为 01-01-2012 00:00:04
Not only the seconds alone should get multiplied; the corresponding minutes, hours, date, month and year also should get multiplied when seconds crosses 59 virtual seconds. That is, the clock should run live with incremental of four seconds for every second with my date format.
不仅秒数应该成倍增加;当秒数超过 59 虚拟秒时,相应的分钟、小时、日期、月份和年份也应该相乘。也就是说,时钟应该以我的日期格式以每秒四秒的增量实时运行。
Please see my page: http://www.chemfluence.org.in/sample.html
请看我的页面:http: //www.chemfluence.org.in/sample.html
It's now running with the current time. I want to run this four times faster.
它现在以当前时间运行。我想把它运行得快四倍。
Please see my code below.
请在下面查看我的代码。
<!DOCTYPE html>
<html>
<head>
<script>
function startTime()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// Add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML =
today.getDate() +
"-" +
(today.getMonth()+1)+"-" +
today.getFullYear() +
" "+h+":"+m+":"+s;
t = setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i = "0" + i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
采纳答案by Bergi
There is a simple formula to determine the virtual time for every given time, knowing the two timestamps and the factor:
有一个简单的公式来确定每个给定时间的虚拟时间,知道两个时间戳和因子:
var virtualOrigin = Date.parse("2012-01-01T00:00:04"),
realOrigin = Date.parse("2012-01-01T00:00:00"),
factor = 4;
function getVirtual(time) {
return new Date( virtualOrigin + (time - realOrigin) * factor );
}
// usage:
var now = new Date(),
toDisplay = getVirtual(now);
回答by Thilo
determine the current time ("START") (as timestamp -- count of seconds since 1970)
when displaying the clock, display (("CURRENT" - "START") * 4) + "START" instead
确定当前时间(“START”)(作为时间戳——自 1970 年以来的秒数)
显示时钟时,改为显示 (("CURRENT" - "START") * 4) + "START"
回答by ericponto
You can do a setInterval
for 1 second and then add 4 seconds to the current date.
您可以执行setInterval
1 秒,然后将当前日期添加 4 秒。
(This example just logs the time to the console, but you can easily hook it up to an HTML element.)
(此示例仅将时间记录到控制台,但您可以轻松地将其连接到 HTML 元素。)
var date = new Date();
setInterval(function(){
date = new Date(date.getTime() + 4000);
console.log(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
}, 1000);