草庐IT

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

coder 2023-10-20 原文

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

这是完整的代码。

<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-jQuery : Display a timer on each table row where end time is not set的更多相关文章

  1. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  2. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  3. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  4. jquery - 如何在 rails 3.1 上安装 jQuery - 2

    我以为它已经安装了,但在我的gemfile中有gem"jquery-rails"但是在我的asset/javascripts文件夹中accounts.js.coffeeapplication.js都被注释掉了这是我的虚拟railsapplication但是在源代码中没有jQuery并且删除链接不起作用......任何想法都丢失了 最佳答案 看看thisRailscast.您可能需要检查application.js文件并确保它包含以下语句。//=requirejquery//=requirejquery_ujs

  5. jquery - Rails 如何创建 Jquery flash 消息而不是默认的 Rails 消息 - 2

    我想在页面顶部创建自定义Jquery消息而不是标准RailsFlash消息。我想在我的投票底部附近创建一条即时消息。我的Controller:defvote_up@post=Post.find(params[:id])current_user.up_vote(@post)flash[:message]='Thanksforvoting!'redirect_to(root_path,:notice=>'Takforditindlæg,deternuonline!')rescueMakeVoteable::Exceptions::AlreadyVotedErrorflash[:error]

  6. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  7. jquery - 在 Rails 中从原型(prototype)切换到 jquery,助手呢? - 2

    我目前从prototype切换到jquery主要是为了支持简单的ajax文件上传。我使用:https://github.com/indirect/jquery-rails95%的javascript代码是由railshelper编写的,例如:-remote_function-render:updatedo|page|-page.replace_html'id',:partial=>'content'-page['form']['name']=something-page.visual_effect:highlight,'head_success'...我知道我必须为Jquery重写5%

  8. jquery - Ruby 1.9.1、Rails 2.3.2 和 jrails 0.4 出现 "rescue in const_missing"错误 - 2

    我最近开始了一个项目,团队决定我们希望使用jQuery而不是Prototype/Scriptaculous来满足我们的javascript需求。我们设置了我们的项目,并开始切换。插件已安装viatheseinstructions,一切都按计划进行。不久之后,当尝试运行“./script/server”时,我们收到以下错误:=>Rails2.3.2applicationstartingonhttp://0.0.0.0:3000/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/depende

  9. jquery - 使用 Rails 3 学习 Ajax 的资源 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。有没有学习Ajax(jQuery)和Rails3的好资源?

  10. jquery - Sprockets::FileNotFound - 找不到文件 'jquery.ui' - 2

    这个问题已经被问过几次了,但我尝试了提供的解决方案,但仍然没有帮助,所以我提出了一个新问题。gem文件gem'jquery-ui-rails'按照建议,我将gem放在:assets组之外Application.css~*=require_self*=requirejquery.ui*=requirebootstrap-datepicker*=requirejquery.timepicker*=require_tree.*/RailsAssetPipeline根据列出的顺序加载Assets。在这里,我把它排在列表的第2位。Application.css.scss*=require_sel

随机推荐