douxia6554 2018-05-30 13:17
浏览 83
已采纳

使用数据库中的数据在表中设置许多倒计时

I would like to set a countdown for each row in my table but my problem is that the only first countdown is displayed in the table and currently I set the end in the code but I want to add the date in my database then trigger the countdown when it's setted.

there is the code in the php file:

<table>

   <tr>
      <td><?php echo $donnees['type'];?></td>
       <td><?php echo $donnees['number'];?></td>
       <td><?php echo $donnees['weight'];?></td>
       //this date is the date of birth
       <td><?php echo $donnees['date'];?></td>
       <td><p id="ctd"></p></td>

   </tr>
</table>

the script to set the countdown :

var countDownDate = new Date("Sep 20, 2018 15:00:00").getTime();

var x = setInterval(function() {


    var now = new Date().getTime();

    var distance = countDownDate - now;


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

    document.getElementById("ctd").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";


    if (distance < 0) {
        clearInterval(x);
        document.getElementById("ctd").innerHTML = "EXPIRED";
    }
}, 1000);
  • 写回答

1条回答 默认 最新

  • douhui2307 2018-05-30 14:09
    关注

    Notice how document.getElementById is singular (getElement and not getElements) getElementById will return the first node with a matching id, so that's why only the first of your timers works. If I'm understanding correctly you have multiple rows and want each one to have an individual countdown from today until the value of $donnees['date'] right? Here's what I would do

    Add some classes

    Unlike ids, multiple elements can have the same class.

    <table>
       <tr class="row_top">
          <td><?php echo $donnees['type'];?></td>
           <td><?php echo $donnees['number'];?></td>
           <td><?php echo $donnees['weight'];?></td>
           //this date is the date of birth
           <td class="col_date"><?php echo $donnees['date'];?></td>
           <td><p class="col_countdown"></p></td>
       </tr>
    </table>
    

    Abstract countdown functionality

    You will be using your date to countdown process many times - why not make it easier for yourself and make it a function?

    function getCountdownText(date)
    {
        var now = new Date().getTime();
    
        var distance = date.getTime() - now; //date is a Date object, so you no longer have to call getTime() on it before passing it to this function
        if(distance < 0) return "EXPIRED";
    
        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);
    
        return days + "d " + hours + "h "+ minutes + "m " + seconds + "s ";
    }
    

    Note that this function now takes a date, so $donnees['date'] should be a valid date string you can pass to new Date()

    Finally, put it all together

    Now you can use a single setinterval running every 1 second to do the countdown generating/updating.

    var run_every=1000; //1000 ms = 1 second
    setInterval(function(){
        var all_rows = document.getElementsByClassName("row_top"); //Notice this says elements - not element - meaning we will be getting an array back.
        for(var i=0;i<all_rows.length;i++)
        {
            var current_row = all_rows[i]; //keep row element so we have a reference to it
    
            var current_date = current_row.getElementsByClassName("col_date")[0].innerText; // get the date from this row's col_date column
            var countdown_text = getCountdownText(new Date(current_date)); //use our function to get the countdown
            current_row.getElementsByClassName("col_countdown")[0].innerText = countdown_text; //set the text of our countdown td
        }
    },run_every);
    

    You can see it in action here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64