簡體   English   中英

倒數計時器未在動態創建的元素內更新

[英]CountDown Timer not updating inside dynamically created element

倒數計時器未在動態創建的元素內更新。 我正在嘗試在ajax成功之后創建的td內倒數計時器。 我將當前服務器時間存儲在變量中。 1000ms后不更新。

這是代碼:

var server_time=`<?php 
                    date_default_timezone_set('Asia/Kolkata');
                    echo date("Y-m-d h:i:s"); 
                    ?>`;
            $.ajax({
                url:'/getCampaigns',
                type:'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success:function(response){

                    $.each(response,function(index,element){
                        $('.append_data').append(`<tr><td>${element.id}</td>
                        <td><a href='/campaign-report?campaign_id=${element.id}'>${element.campaign_name}</a></td>
                        <td>${element.total_numbers}</td>
                        <td id="countdown_${element.id}"></td>
                        <td>${element.start_time}</td>
                        </tr>`);

                        /** Countdown Start *****/
                        if(element.status=="Scheduled")
                        {
                            var countDownDate = new Date(`${element.start_time}`).getTime();


                            var x = setInterval(function() {

                            // Get todays date and time
                            var now = new Date(`${server_time}`).getTime();


                            // Find the distance between now and 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"
                            $("#countdown_"+element.id).html(days + "d " + hours + "h "
                            + minutes + "m " + seconds + "s ");

                            // If the count down is finished, write some text
                            if (distance < 0) {
                            clearInterval(x);
                            document.getElementById("countdown_"+element.id).innerHTML="Started";
                            }
                            }, 1000);
                            }

現在正在工作。 我刪除了php代碼並使用了js new Date();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM