[英]javascript countdown script from milliseconds in to hours-minutes-seconds
我正在寻找一种简单的方法来输出毫秒到小时 - 分钟 - 秒。 我见过一些插件,但我不想在插件中使用插件,我看过很多倒计时片段,但这些是使用日期,我使用的不是日期而是毫秒。
谢谢。
编辑:我正在寻找一个从xxxx毫秒倒数到零的脚本
//到现在为止我有这个
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 );
简单的数学。
1000毫秒= 1秒。
60秒= 1分钟。
60分钟= 1小时。
所以如果你有123456789毫秒,你会做类似的事情:
123456789/1000(秒),即123456.789
123456.789 / 60(至分钟),即2,057.6
2,057.6 / 60(至小时),即34.29小时。
然后使用余数(.2935525)并将其转换为分钟 - 这是17.61315
再次使用余数(.61315)并将其转为秒...即36.789
因此,简而言之,123456789毫秒等于34小时17分37秒。
我相信你可以自己做JavaScript部分。
好吧,也许你还需要一些帮助。
演示 : 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);
此外,为了进一步阅读 - setInterval可能会漂移。 所以不要指望这是100%准确。 setInterval会漂移吗?
我不确定你要求的是什么,但......
var hours = Math.floor(millis / 36e5),
mins = Math.floor((millis % 36e5) / 6e4),
secs = Math.floor((millis % 6e4) / 1000);
请查看: http : //code.google.com/p/jquery-countdown/
我用过这个。
在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'
});
您可以使用上面显示的功能获得时间...我需要在计时器结束时提交表单...如果需要,您可以执行类似的操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.