gpt4 book ai didi

javascript - 如何完成倒计时0秒?

转载 作者:行者123 更新时间:2023-12-03 12:07:05 25 4
gpt4 key购买 nike

如何完成倒计时至0秒?

倒计时完成后,将显示00days00hours00mins01seconds

我想像这样显示 0 秒 00days00hours00mins00seconds 我该怎么做?

http://jsfiddle.net/D3E9G/6/

最佳答案

给你

(function($) {
$.fn.countdown = function(options) {
// default options
var defaults = {
attrName : 'data-diff',
tmpl : '<span class="hour">%{h}</span><span class="minute">%{m}</span>minutes<span class="second">%{s}</span>seconds',
end : 'has ended',
afterEnd : null
};
options = $.extend(defaults, options);


// trim zero
function trimZero(str) {
return parseInt(str.replace(/^0/g, ''));
}
// convert string to time
function getDiffTime(str) {
var m;
if ((m = /^(\d{4})[^\d]+(\d{1,2})[^\d]+(\d{1,2})\s+(\d{2})[^\d]+(\d{1,2})[^\d]+(\d{1,2})$/.exec(str))) {
var year = trimZero(m[1]),
month = trimZero(m[2]) - 1,
day = trimZero(m[3]),
hour = trimZero(m[4]),
minute = trimZero(m[5]),
second = trimZero(m[6]);
return Math.floor((new Date(year, month, day, hour, minute, second).getTime() - new Date().getTime()) / 1000);
}
return parseInt(str);
}
// format time
function format(diff) {
var tmpl = options.tmpl, day, hour, minute, second;
day = /%\{d\}/.test(tmpl) ? Math.floor(diff / 86400) : 0;
hour = Math.floor((diff - day * 86400) / 3600);
minute = Math.floor((diff - day * 86400 - hour * 3600) / 60);
second = diff - day * 86400 - hour * 3600 - minute * 60;

day = ("00" + day).substr(-2);
hour = ("00" + hour).substr(-2);
minute = ("00" + minute).substr(-2);
second = ("00" + second).substr(-2);

tmpl = tmpl.replace(/%\{d\}/ig, day)
.replace(/%\{h\}/ig, hour)
.replace(/%\{m\}/ig, minute)
.replace(/%\{s\}/ig, second);
return tmpl;
}
// main
return this.each(function() {
var el = this,
diff = getDiffTime($(el).attr(options.attrName));
function update() {
if (diff < 0) {
if (options.afterEnd) {
options.afterEnd();
}
return;
}
$(el).html(format(diff));
setTimeout(function() {
diff--;
update();
}, 1000);
}
update();
});
};
})(jQuery);





$(function() {
$('.J_countdown2').countdown({
tmpl : '<span>%{d}</span>days<span>%{h}</span>hours<span>%{m}</span>minutes<span>%{s}</span>seconds'
});
});

关于javascript - 如何完成倒计时0秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136859/

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