gpt4 book ai didi

JavaScript 从时间戳开始计数

转载 作者:行者123 更新时间:2023-11-30 10:24:25 25 4
gpt4 key购买 nike

我有这个计数器,我想从特定时间戳 (1385132831.818785) 而不是 0 开始。我该怎么做?

startTimer: function(el) {      

var counter = 0,
cDisplay = $(el);
var format = function(t) {

var minutes = Math.floor(t/600),
seconds = Math.floor( (t/10) % 60);
minutes = (minutes === 0) ? "" : (minutes === 1)? minutes.toString() + ' min ' : minutes.toString() + ' mins ';
seconds = (seconds === 0) ? "" : seconds.toString() + ' secs';
cDisplay.html(minutes + seconds);
};
setInterval(function() {
counter++;
format(counter);
},100);

}

最佳答案

尝试

var el = '.timer';
var start = 1385132831,
cDisplay = $(el);
var format = function (t) {
var hours = Math.floor(t / 3600),
minutes = Math.floor(t / 60 % 60),
seconds = Math.floor(t % 60),
arr = [];
if (hours > 0) {
arr.push(hours == 1 ? '1 hr' : hours + 'hrs');
}
if (minutes > 0 || hours > 0) {
arr.push(minutes > 1 ? minutes + ' mins' : minutes + ' min');
}
if (seconds > 0 || minutes > 0 || hours > 0) {
arr.push(seconds > 1 ? seconds + ' secs' : seconds + ' sec');
}
cDisplay.html(arr.join(' '));
};
setInterval(function () {
format(new Date().getTime() / 1000 - start);
}, 1000);

演示:Fiddle

关于JavaScript 从时间戳开始计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20148610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com