PHP-jQuery : Display a timer on each table row where end time is not set

标签 php jquery mysql ajax

我有一个显示计数计时器的函数,但我只能让它对单行起作用。我想让一个计时器出现在尚未设置操作结束时间的每一行上。

这是完整的代码。

<script>
$('document').ready(function()
{
var uni_id = //value of id needed to query the database table;
    $.ajax({
    type : 'POST',
    url  : 'get_time.php',
    dataType: 'json',
    data : {uni_id: uni_id},
    cache: false,
    success :  function(result)
        {
        for (var i = 0; i < result.length; i++){

            var startDateTime = new Date(result[i].uni_start_time); //Not sure if it's wrong to add all returning values here but so far it works but only for a single row on the table.
            var startStamp = startDateTime.getTime();
            var newDate = new Date();
            var newStamp = newDate.getTime();
            var timer;
            var rec_id = result[i].rec_id;


            function updateTimer() {
                newDate = new Date();
                newStamp = newDate.getTime();
                var diff = Math.round((newStamp-startStamp)/1000);

                var days = Math.floor(diff/(24*60*60));
                diff = diff-(days*24*60*60);
                var hours = Math.floor(diff/(60*60));
                diff = diff-(hours*60*60);
                var minutes = Math.floor(diff/(60));
                diff = diff-(minutes*60);
                var seconds = diff;

                days = (String(days).length >= 2) ? days : '0' + days;
                hours = (String(hours).length >= 2) ? hours : '0' + hours;
                minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
                seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;  

                $("#on_going_"+rec_id).html(hours+':'+minutes+':'+seconds);
            }
            setInterval(updateTimer, 1000);


            $('#uni_time_table tbody').append(
                '<tr>'
                +'<td class="center hidden" id="'+result[i].rec_id+'">' + result[i].rec_id + '</td>'
                +'<td class="center">' + result[i].uni_id + '</td>'
                +'<td>' + result[i].uni_name + '</td>'
                +'<td>' + result[i].uni_loc + '</td>'
                +'<td class="center">' + result[i].uni_date + '</span></td>'
                +'<td class="center">' + result[i].uni_start_time + '</td>'
                +(result[i].uni_end_time == null ?
                '<td class="center"></td>' //this will always be empty if the uni_end_time is not set.
                :
                '<td class="center">' + result[i].uni_end_time + '</td>'
                )
                +(result[i].uni_end_time == null ?
                '<td class="center" id="on_going_'+result[i].rec_id+'"></td>' //the timer will only be triggered here if the uni_end_time is not set.
                :
                '<td class="center">' + result[i].uni_total_time + '</td>' //this will display the total time as long as the uni_end_time is set.
                )
                +'</tr>');
            }
        }
    });
});
</script>

到目前为止,只有表中的第一个条目显示计时器。每次我添加另一个条目时,第二个条目只是保持空白,而计时器仍然与第一个条目一起工作。

如何让计时器为表格中的每一行工作?

编辑:来自 json 的样本数据

rec_id              "1"
uni_date            "2016-10-28"
uni_loc             "Custom Location"
uni_id              "2356"
uni_name            "Custom Name"
uni_start_time      "10/28/2016 09:04:28"
uni_end_time        null
uni_total_time      null // this shows null because end_time is empty which is when the timer should kick in.

最佳答案

这里有一个解决方案,只需对您的代码进行少量修改:

var startStamp = [];

function updateTimer(result) {

    var newDate = new Date();
    var newStamp = newDate.getTime();

    for (var j = 0; j < result.length; j++) {
        newDate = new Date();
        newStamp = newDate.getTime();
        var diff = Math.round((newStamp-startStamp[j])/1000);

        var days = Math.floor(diff/(24*60*60));
        diff = diff-(days*24*60*60);
        var hours = Math.floor(diff/(60*60));
        diff = diff-(hours*60*60);
        var minutes = Math.floor(diff/(60));
        diff = diff-(minutes*60);
        var seconds = diff;

        days = (String(days).length >= 2) ? days : '0' + days;
        hours = (String(hours).length >= 2) ? hours : '0' + hours;
        minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
        seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;

        $("#on_going_"+result[j].rec_id).html(hours+':'+minutes+':'+seconds);
    }
}

$('document').ready(function()
{
        var uni_id = 1;//
        $.ajax({
            type : 'POST',
            url  : 'get_time.php',
            dataType: 'json',
            data : {uni_id: uni_id},
            cache: false,
            success :  function(result) {

                for (var i = 0; i < result.length; i++) {
                    var startDateTime = new Date(result[i].uni_start_time);
                    startStamp[i] = startDateTime.getTime();

                    $('#uni_time_table tbody').append(
                        '<tr>'
                        + '<td class="center hidden" id="' + result[i].rec_id + '">' + result[i].rec_id + '</td>'
                        + '<td class="center">' + result[i].uni_id + '</td>'
                        + '<td>' + result[i].uni_name + '</td>'
                        + '<td>' + result[i].uni_loc + '</td>'
                        + '<td class="center">' + result[i].uni_date + '</span></td>'
                        + '<td class="center">' + result[i].uni_start_time + '</td>'
                        + (result[i].uni_end_time == null ?
                            '<td class="center"></td>' //this will always be empty if the uni_end_time is not set.
                            :
                        '<td class="center">' + result[i].uni_end_time + '</td>'
                        )
                        + (result[i].uni_end_time == null ?
                        '<td class="center" id="on_going_' + result[i].rec_id + '"></td>' //the timer will only be triggered here if the uni_end_time is not set.
                            :
                        '<td class="center">' + result[i].uni_total_time + '</td>' //this will display the total time as long as the uni_end_time is set.
                        )
                        + '</tr>');
                }

                setInterval((function () {
                    updateTimer(result);
                }), 1000);
            }
        });
});

关于PHP-jQuery : Display a timer on each table row where end time is not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292749/

相关文章:

php - 购物车商品价格计算,基于 Woocommerce 中选择的 "days"自定义字段

php - DoAuthorization 返回 "Permission denied"错误

javascript - 基于 JSON 添加表行

javascript - 如何多次触发 '.click' 事件?

mysql - 如何在查询中使用两列的 GROUP_CONCAT

Mysql 与未知列匹配

php - 使用php将位图从服务器下载到android

jquery - 拖动并旋转容器内的图像

javascript - CSS/HTML 中的 div 网格对应于 javascript 中的二维数组

javascript - 在网站页面之间传输数据