dqzow3859 2017-10-22 14:32
浏览 51

PHP多个计时器函数实例

I'm making a site where users post text posts and each post has a countdown timer on, different for each one. I am having a problem where I am only able to have one timer running.

I have a function to get the time depending on the time in the database for the post and the current time. The php file loops through each post and calls the time function to get the time for each post. However it is only creating one timer.

code for the timer

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>";
    }

code to get time in each post

function getPosts(){

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

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

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

it is only displaying the time for the first post in the loop

  • 写回答

2条回答 默认 最新

  • dongshan1396 2017-10-23 20:10
    关注

    Try using

    class="demo" 
    

    instead of

    id="demo"
    

    The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). Check How to use HTML id Attribute

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置