JavaScript setIntervall只运行一次
此代码仅在1秒后执行一次倒计时功能。我正在尝试实现一个简单的倒计时,扩展一个不再兼容的wordpress插件。我对JavaScript完全是初学者,上面的所有内容都是100%复制和粘贴,但除了自动调用函数外,其他都可以使用。因为所有代码都包装在jQuerydocument.ready方法中,只有在文档准备就绪时才会执行,并且根据定义只能执行一次。按如下方式移动init函数:JavaScript setIntervall只运行一次,javascript,setinterval,Javascript,Setinterval,此代码仅在1秒后执行一次倒计时功能。我正在尝试实现一个简单的倒计时,扩展一个不再兼容的wordpress插件。我对JavaScript完全是初学者,上面的所有内容都是100%复制和粘贴,但除了自动调用函数外,其他都可以使用。因为所有代码都包装在jQuerydocument.ready方法中,只有在文档准备就绪时才会执行,并且根据定义只能执行一次。按如下方式移动init函数: <script> function countdown(){ jQuery(docu
<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>
jQuerydocument.ready只调用一次摆脱jQuerydocument.readyfunction${…你会没事的。现在的情况是,你每1000毫秒只注册一次事件,但是很明显,DOMReady事件只会触发一次。你最好在$document.ready上调用setIntervalcountdown,1000,尽管你不明白jQuerydocument.ready的作用。@haim770 ready回调会在页面打开时立即执行已经准备好了。这很奇怪,但不是问题。什么是$'.countdown'.countdown…?是否加载了jQuery插件?可能的解决方案:将``jQuerydocument.readyfunction${``行替换为:if document.readyState!=='complete'{return;}并删除};最后,当然。不幸的是,这不起作用。当插入alertTest时,弹出窗口确实每秒都会出现,但是更新文本所需的功能不会出现。因此,我想现在的问题不再是SetInterval,而是它内部的功能,当调用multiple tim时,它的工作方式与预期不符很遗憾,我对JavaScript一无所知。你应该打开一个新的问题并添加代码。或者创建一个提琴。。。
<script>
jQuery(document).ready(function($){
function countdown(){
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>