Ign*_*nas 8 javascript jquery momentjs
我找到了很酷的Momentjs库,但是我没有找到关于如何实现一些简单任务的文档.我正在尝试构建一个倒数计时器,我猜我应该使用持续时间对象,但我不太明白(可能是因为英语不是我的第一语言).无论如何这是我想要的:
var time = 7200;
var duration = moment.duration('seconds',time);
setInterval(function(){
//show how many hours, minutes and secods are left
$('.countdown').text(duration.format('h:mm:ss'));
//this doesn't work because there's no method format for the duration object.
},1000);
Run Code Online (Sandbox Code Playgroud)
所以它应该显示每一秒:
02:00:00
1时59分59秒
1时59分58秒
1时59分57秒
...
00:00:00
如何使用Momentjs库实现此结果?谢谢!
And*_*rei 14
durationobject表示静态期间,并且不会随着时间的推移而增加/减少.因此,如果你要减少它,你必须自己做,例如创建一种秒计数器或duration每次重新创建对象.这是第二个选项的代码:
var time = 7200;
var duration = moment.duration(time * 1000, 'milliseconds');
var interval = 1000;
setInterval(function(){
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
//show how many hours, minutes and seconds are left
$('.countdown').text(moment(duration.asMilliseconds()).format('h:mm:ss'));
}, interval);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13471 次 |
| 最近记录: |