gpt4 book ai didi

jquery - 如何实现倒计时功能?

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:49 27 4
gpt4 key购买 nike

我制作了一个倒数计时器并使用 HTML 和 CSS 对其进行了编码,但我无法使用 jQuery 使其发挥作用。我怎样才能实现这个倒计时?这是我更喜欢倒计时的 HTML 结构:

    <div id="counter-container">
<div class="counter-box">
<div class="counter"><p class="counter-p">#</p></div>
<p class="counter-box-p">days</p>
</div>
<div class="counter-box">
<div class="counter"><p class="counter-p">#</p></div>
<p class="counter-box-p">hours</p>
</div>
<div class="counter-box">
<div class="counter"><p class="counter-p">#</p></div>
<p class="counter-box-p">minutes</p>
</div>
<div class="counter-box">
<div class="counter"><p class="counter-p">#</p></div>
<p class="counter-box-p">seconds</p>
</div>

如您所见,我已经为天、小时等添加了段落元素,因此我不想通过 jQuery 嵌入任何额外的文本。

附言谁能告诉我如何设置新的倒计时日期?

最佳答案

function counter(count) {
countdown = setInterval(function(){
var temp;
$("p#d").html(Math.floor(count/(60*60*24)));
temp = count%(60*60*24);
$("p#h").html(Math.floor(temp/(60*60)));
temp = count%(60*60);
$("p#m").html(Math.floor(temp/(60)));
temp = count%(60);
$("p#s").html(temp);
if (count == 0) {
alert("time's up");
clearInterval(countdown);
}
count--;
}, 1000);
}

counter(60*60*24*2);

demo

EDIT-1:

计数器以秒为单位获取时间。

counter(10); //10 seconds
counter(10*60) //600 seconds -> 10 minutes.
counter(10*60*60) //36000 seconds -> 600 minutes -> 10 hour

EDIT-2:

如果你想让它基于Date工作,你应该像这样改变函数,

function counter(futureDate) {
var today = new Date(); // today
count = Math.floor((futureDate-today)/1000);
countdown = setInterval(function(){
var temp;
$("p#d").html(Math.floor(count/(60*60*24)));
temp = count%(60*60*24);
$("p#h").html(Math.floor(temp/(60*60)));
temp = count%(60*60);
$("p#m").html(Math.floor(temp/(60)));
temp = count%(60);
$("p#s").html(temp);
if (count == 0) {
alert("time's up");
clearInterval(countdown);
}
count--;
}, 1000);
}

counter(new Date(2012,4,8)); // May 8, 2012 00:00:00
/* counter(new Date(2012,4,8,15,49,10)); // May 8, 2012 15:49:00 */

http://jsfiddle.net/NfLAB/1/

关于jquery - 如何实现倒计时功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049368/

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