javascript 倒计时脚本从毫秒到小时-分钟-秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11461971/
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 countdown script from milliseconds in to hours-minutes-seconds
提问by user759235
I am looking for a easy way to output milliseconds into hours-minutes-seconds. I have seen some plugins but i dont want to use a plugin in a plugin, and I have seen a lot of countdown snippets but these are using the date, I am using not a date but milliseconds.
我正在寻找一种将毫秒输出为小时-分钟-秒的简单方法。我看过一些插件,但我不想在插件中使用插件,我也看过很多倒计时片段,但这些都是使用日期,我使用的不是日期而是毫秒。
Thanks.
谢谢。
EDIT: i am looking for a script that counts back from xxxx milliseconds to zero
编辑:我正在寻找一个从 xxxx 毫秒倒数到零的脚本
//until now i have this
//直到现在我有这个
diff = 100000000;
function showTimer() {
hours = Math.floor( diff / (1000*60*60) );
mins = Math.floor( diff / (1000*60) );
secs = Math.floor( diff / 1000 );
$('.count').html(hours+ 'hours' +minutes + 'minutes' +seconds + ' seconds');
}
setInterval(function(){showTimer()}, 1000 );
回答by ahren
Simple maths.
简单的数学。
1000 milliseconds = 1 second.
1000 毫秒 = 1 秒。
60 seconds = 1 minute.
60 秒 = 1 分钟。
60 minutes = 1 hour.
60 分钟 = 1 小时。
So if you have 123456789 milliseconds, you'd do something like:
因此,如果您有 123456789 毫秒,您可以执行以下操作:
123456789 / 1000 (to seconds) which is 123456.789
123456789 / 1000(秒),即 123456.789
123456.789 / 60 (to minutes) which is 2,057.6
123456.789 / 60(到分钟),即 2,057.6
2,057.6 / 60 (to hours) which is 34.29 hours.
2,057.6 / 60(到小时),即 34.29 小时。
Then use the remainder (.2935525) and turn this into minutes - which is 17.61315
然后使用余数 (.2935525) 并将其转换为分钟 - 即 17.61315
Use the remainder again (.61315) and turn it to seconds... which is 36.789
再次使用余数 (.61315) 并将其转换为秒...即 36.789
So, in short, 123456789 milliseconds equals 34 hours, 17 minutes and 37 seconds.
因此,简而言之,123456789 毫秒等于 34 小时 17 分 37 秒。
I'm sure you can do the JavaScript part yourself.
我相信您可以自己完成 JavaScript 部分。
Okay maybe you need some more help.
好吧,也许你需要更多帮助。
DEMO: http://jsbin.com/eqawam/edit#javascript,html,live
演示:http: //jsbin.com/eqawam/edit#javascript,html,live
var millis = 123456789;
function displaytimer(){
//Thank you MaxArt.
var hours = Math.floor(millis / 36e5),
mins = Math.floor((millis % 36e5) / 6e4),
secs = Math.floor((millis % 6e4) / 1000);
$('.count').html(hours+':'+mins+':'+secs);
}
setInterval(function(){
millis -= 1000;
displaytimer();
}, 1000);
Also, for further reading - setInterval may drift. So don't expect this to be 100% accurate. Will setInterval drift?
此外,为了进一步阅读 - setInterval 可能会漂移。所以不要指望这是 100% 准确的。setInterval 会漂移吗?
回答by MaxArt
I'm not sure about what you're asking for, but...
我不确定你在问什么,但是......
var hours = Math.floor(millis / 36e5),
mins = Math.floor((millis % 36e5) / 6e4),
secs = Math.floor((millis % 6e4) / 1000);
回答by jsshah
Take a look at : http://code.google.com/p/jquery-countdown/
看看:http: //code.google.com/p/jquery-countdown/
I have used this.
我用过这个。
In Javascript you can do the following
在 Javascript 中,您可以执行以下操作
$('#counter_2').countdown({
image: 'img/digits.png',
startTime: '10:59',
timerEnd: function(){
alert('The Time Limit for the quiz has expired. Click OK to submit quiz');
$('#submit_quiz_form').submit();
},
format: 'mm:ss'
});
You can get the time using the functions shown above ... I needed a form to be submitted when the timer ends ... you can do something similar if needed
你可以使用上面显示的函数来获取时间......我需要在计时器结束时提交一个表单......如果需要你可以做类似的事情