2017-10-22 5 views
0

私は、ユーザーがテキスト投稿を投稿し、それぞれの投稿にそれぞれ異なるカウントダウンタイマーを設定しているサイトを作っています。私は1つのタイマーしか動かすことができないという問題を抱えています。PHP mutiple timer関数のインスタンス

私は、ポストと現在の時間のデータベースの時間に応じて時間を取得する機能を持っています。 phpファイルは各投稿をループし、time関数を呼び出して各投稿の時刻を取得します。ただし、1つのタイマーしか作成していません。タイマー

function getTime($conn, $pid){ 
     $countDownDate; 
     $sql = "SELECT * FROM userposts"; 
     $result = mysqli_query($conn,$sql); 
     while ($row = $result->fetch_assoc()){ 
      if ($row['pid'] == $pid){ // getting the row of the user 
       $countDownDate = $row['time']; 
      } 
     } 
     echo "<p style='color:black' id='demo'></p> 
      <script> 

      // Set the date we're counting down to 
      var countDownDate = new Date('".$countDownDate."'); 

      // Update the count down every 1 second 
      var x = setInterval(function() { 

       // 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); 

       // Display the result in the element with id='demo' 
       document.getElementById('demo').innerHTML = days + 'd ' + hours + 'h ' 
       + minutes + 'm ' + seconds + 's '; 

       // If the count down is finished, write some text 
       if (distance < 0) { 
       clearInterval(x); 
       document.getElementById('demo').style.color = 'red'; 
       document.getElementById('demo').style.fontWeight = '900'; 
       document.getElementById('demo').innerHTML = 'EXPIRED'; 
       } 
      }, 1000); 
      </script></br>"; 
    } 

コードの

コードは、それぞれが、それが唯一のループで最初のポストのための時間を表示している

function getPosts(){ 

    while ($row = $result->fetch_assoc()){ 
     echo "<div class='post-box'><p>"; 

     // displaying time 
     getTime($conn,$row['pid']); 

     echo "</p></div>"; 
    } 
} 

投稿に時間を取得するために

答えて

1

を使用してみてください
class="demo" 

の代わりに、

id="demo" 

id属性は、HTML要素(HTML文書内で一意でなければなりません値 )の一意のIDを指定します。 チェック How to use HTML id Attribute

関連する問題