2017-03-21 520 views
-1
<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只运行一次

+0

摆脱。就绪的'jQuery的(文件)(函数($){...',你会现在的方法是,每个'1000ms'只注册一次事件,但显然DOMReady事件只会触发一次,最好在调用setInterval(countdown,1000)后'$(document.ready)'虽然 – haim770

+1

你不明白'jQuery(document).ready'的作用是什么 – dfsq

+0

@ haim770当ready页面是已经准备好了。这很奇怪,但不是问题。 – Bergi

回答

-1

由于您的所有代码都被封装在jQuery(document).ready()方法中,所以只有在文档准备好并且根据定义只能发生一次时,它才会执行。

-1

移动你的初始化函数如下:

<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> 

jQuery(document).ready只调用一次

+0

不幸的是,这不起作用。插入警报(“测试”)时;弹出窗口确实每秒都会出现,但更新文本的所需功能不会。所以我想现在问题不再是setIntervall了,而是它内部的功能,当多次调用时,它不会以预期的方式工作。可悲的是,我对JavaScript没有任何知识。 – user3752313

+0

我创建了这个小提琴,请检查:https://jsfiddle.net/5tp3r5ez/ – MemLeak

+0

你应该打开一个新的问题,并添加代码。或创建一个小提琴... – MemLeak