【发布时间】:2016-03-15 04:48:58
【问题描述】:
我想在每个 div 中显示倒计时。现在我从我的数据库中获取秒数并存储在 data-countdown 属性中,然后使用以下 js 代码进行倒计时。只有第一个 div 每秒更改一次值,其他的不会更改。 这是小提琴: http://fiddle.jshell.net/j61qs7oc/
//imagine this line of code in every loop of a for loop so $remaining will be different
<div style="font-size: 25px; color:#e3b40b ; font-weight: 600;" data-countdown="'.$remaining.'"><i class="fa fa-spinner fa-pulse"></i></div>
这里是js代码
$('[data-countdown]').each(function() {
finalDate = $(this).data('countdown');
var $this = $(this);
timeout = null;
time = null;
startCountdown($this,finalDate, 1000, end);
function startCountdown(display,timen, pause, callback) {
time = timen;
display.html(timen);
if (timen == 0)
callback();
else {
clearTimeout(timeout);
timeout = setTimeout(function () {
startCountdown(display,timen - 1, pause, callback)
}, pause);
}
}
function end() {
alert();
}
});
【问题讨论】:
-
你想做什么?..你能解释一下吗
-
@SunnyS.M 看看我的小提琴。我希望每个 div 有一个单独的倒计时
-
嗯,我来晚了..它已经解决了
-
这里(fiddle.jshell.net/j61qs7oc/2)我更新了代码,但已经被 Brandon Gano 解决了......
标签: javascript jquery html jquery-countdown