间隔不更新文本 jQuery
Interval doesn't update text jQuery
我正在使用 jQuery 和 setInterval 在我的元素中显示倒计时,但它不会更新文本但是使用 console.log 我可以看到间隔是 运行 正确.这是代码:
我在 jQuery:
中调用这个函数
function startTimer(duration, display) {
var timer = duration, minutes, seconds,days,hours;
setInterval(function () {
days = Math.floor(duration / (60*60*24));
days_raw = days * (60*60*24);
hours = Math.floor((duration-days_raw) / (60*60));
hours_raw = hours * (60*60);
minutes = Math.floor((duration-(hours_raw + days_raw)) / (60));
minutes_raw = minutes * (60);
seconds = Math.floor(duration-(hours_raw + days_raw + minutes_raw));
seconds_raw = seconds;
if(days>0)
{
if(days===1){
display.text(days+" Day");
}
else
{
display.text(days+" Days");}
}
else if(days<=0)
{ console.log(display);
display.text(hours+":"+minutes + ":" + seconds);
}
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
这是我的 jQuery 代码:
jQuery(document).ready(function() {
$('[data-countdown]').each(function() {
var $this = $(this), finalDate = $(this).data('countdown');
startTimer(finalDate,$this);
});
});
.data("countdown") 的值来自 php 文件
问题是 Duration 的值没有改变,所以我添加了 duration--; if(duration<0) return;
并解决了问题
我正在使用 jQuery 和 setInterval 在我的元素中显示倒计时,但它不会更新文本但是使用 console.log 我可以看到间隔是 运行 正确.这是代码: 我在 jQuery:
中调用这个函数function startTimer(duration, display) {
var timer = duration, minutes, seconds,days,hours;
setInterval(function () {
days = Math.floor(duration / (60*60*24));
days_raw = days * (60*60*24);
hours = Math.floor((duration-days_raw) / (60*60));
hours_raw = hours * (60*60);
minutes = Math.floor((duration-(hours_raw + days_raw)) / (60));
minutes_raw = minutes * (60);
seconds = Math.floor(duration-(hours_raw + days_raw + minutes_raw));
seconds_raw = seconds;
if(days>0)
{
if(days===1){
display.text(days+" Day");
}
else
{
display.text(days+" Days");}
}
else if(days<=0)
{ console.log(display);
display.text(hours+":"+minutes + ":" + seconds);
}
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
这是我的 jQuery 代码:
jQuery(document).ready(function() {
$('[data-countdown]').each(function() {
var $this = $(this), finalDate = $(this).data('countdown');
startTimer(finalDate,$this);
});
});
.data("countdown") 的值来自 php 文件
问题是 Duration 的值没有改变,所以我添加了 duration--; if(duration<0) return;
并解决了问题