<script>
function countdown(){
jQuery(document).ready(function($){
var countdown_html = $('.countdown').clone();
var countDownDate = new Date("Apr 23, 2017 19:37:25").getTime();
// Update the count down every 1 second
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance/(1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60));
var seconds = Math.floor((distance % (1000 * 60))/1000);
$('.num.days', countdown_html).text(days);
$('.num.hours', countdown_html).text(hours);
$('.num.minutes', countdown_html).text(minutes);
$('.num.seconds', countdown_html).text(seconds);
$('.countdown').countdown({until: <?php echo $date['to'] ?>, layout: countdown_html.html() });
});
}
var run = setInterval(countdown,1000);
</script>
此代码仅在1秒后执行一次倒计数功能。我试图实现一个简单的倒数计时器,扩展了一个不兼容的wordpress插件。我是一个JavaScript初学者,以上都是100%复制&粘贴,但它都可以工作,除了自动调用函数。JavaScript setIntervall只运行一次
摆脱。就绪的'jQuery的(文件)(函数($){...',你会现在的方法是,每个'1000ms'只注册一次事件,但显然DOMReady事件只会触发一次,最好在调用setInterval(countdown,1000)后'$(document.ready)'虽然 – haim770
你不明白'jQuery(document).ready'的作用是什么 – dfsq
@ haim770当ready页面是已经准备好了。这很奇怪,但不是问题。 – Bergi