2016-12-28 54 views
1

你好我想添加多個倒數計時器內通過JavaScript,但一些如何,它不工作,任何一個可以告訴我,我做錯了,我是一個新的蜜蜂。JavaScript的倒計時PHP的循環不工作

下面

是代碼:

php while loop { 

    <script> 
    var timer; 

    var days= <?php echo $datediff ; ?>;  

    var compareDate = new Date(); 
    compareDate.setDate(compareDate.getDate() + <?php echo $datediff ?>); //just for this demo today + 7 days 

    timer = setInterval(function() { 
     timeBetweenDates(compareDate); 
    }, 1000); 

    function timeBetweenDates(toDate) { 
     var dateEntered = toDate; 
     var now = new Date(); 
     var difference = dateEntered.getTime() - now.getTime(); 

     if (difference <= 0) { 
      // Timer done 
      clearInterval(timer); 
     } else { 
      var seconds = Math.floor(difference/1000); 
      var minutes = Math.floor(seconds/60); 
      var hours = Math.floor(minutes/60); 
      var days = Math.floor(hours/24); 

      hours %= 24; 
      minutes %= 60; 
      seconds %= 60; 

      $(".days") .text(days); 
      $(".hours").text(hours); 
      $(".minutes").text(minutes); 
      $(".seconds").text(seconds); 
     }  
    } 
    </script> 
    <div class="timer"> 
     <span class="days"></span>days 
     <span class="hours"></span>hours 
     <span class="minutes"></span>minutes 
     <span class="seconds"></span>seconds 
    </div> 

我不能讓計時器工作已經改變了ID上課。

+0

嘿,我想和它爲我工作。您是否在瀏覽器的開發工具中檢查了控制檯是否有任何錯誤?在這裏顯示完整的代碼,我會盡力幫助你找出問題所在。 – tiomno

+0

Iam試圖在while循環內工作,在這裏我獲取產品信息。 –

+0

保持在讓我把完整的代碼 –

回答

1

如果我的想法正確地在這裏你需要用全局和局部變量這裏沒有起到覆蓋在每一個循環在全球範圍內的值。

檢查這個代碼,讓我知道,如果它可以幫助你以某種方式。

<?php 
$datediffs = [7, 8, 9, 10]; //let's say this is the array of results from the database 
?> 
<html> 
<head> 
    <script src="//code.jquery.com/jquery-3.1.1.min.js"></script> 
</head> 
<body> 
<script> 
    var timer = [] 
    var timestamp = new Date() 

    function timeBetweenDates(toDate, key) { 
     var now = new Date() 
     var difference = toDate.getTime() - now.getTime() 

     if (difference <= 0) 
     { 
      // Timer done 
      clearInterval(timer[key]); 
     } 
     else 
     { 
      var seconds = Math.floor(difference/1000) 
      var minutes = Math.floor(seconds/60) 
      var hours = Math.floor(minutes/60) 
      var days = Math.floor(hours/24) 

      hours %= 24 
      minutes %= 60 
      seconds %= 60 

      var $timer = $('.timer-' + key) 

      $timer.find('.days').text(days) 
      $timer.find('.hours').text(hours) 
      $timer.find('.minutes').text(minutes) 
      $timer.find('.seconds').text(seconds) 
     } 
    } 
</script> 

<?php foreach ($datediffs as $key => $datediff) : ?> 
    <script> 
     timer.push(setInterval(function() 
     { 
      var compareDate = new Date() 
      compareDate.setTime(timestamp.getTime()) 
      compareDate.setDate(compareDate.getDate() + <?php echo $datediff ?>) 
      timeBetweenDates(compareDate, <?php echo $key; ?>) 
     }, 1000)) 
    </script> 

    <div class="timer-<?php echo $key; ?>"> 
     <span class="days"></span> days 
     <span class="hours"></span> hours 
     <span class="minutes"></span> minutes 
     <span class="seconds"></span> seconds 
    </div> 
<?php endforeach; ?> 
</body> 
</html> 
+0

你可以使用MySQL替換爲每個循環而循環,這樣會很容易讓我明白了嗎? –

+0

而($行= mysql_fetch_assoc($ CONSULTA)){?> $ DATEDIFF = $ _row [ '天']} –

+0

確定我具有存儲在分貝兩個日期1. 2017年12月27日(截止日期)2 2017-12 -26(時間戳)現在怎麼辦是從 –