简体   繁体   English

jQuery-刷新getJson调用

[英]jQuery - refresh getJson call

Ok, so I'm using the below JS to query an API and populate a table. 好的,所以我使用下面的JS查询API并填充表格。 As per another post I saw on SO on each iteration of the loop in the success function it builds an array and then joins it in jQuery and appends it as a row in the table. 在另一篇文章中,我在成功函数的循环的每次迭代中都看到了SO,它构建了一个数组,然后将其加入jQuery,并将其附加为表中的一行。 This works cool: 这很酷:

   // get the JSON from the API
    $.getJSON( "helpers.php", {
        func: "results",
        days: "3",
    })
    .done(function( rows ) {
        $.each(rows, function() {
            // populate the table
            var r = new Array(), j = -1;
            var matchDate = this.date;
                r[++j] = '<tr>';
                r[++j] = '<td><small>' + this.teamname; + '</small></td>';
                r[++j] = '<td>' + this.teamscore; + '</td>';
                r[++j] = '</tr>';
            $('#table-results').append(r.join('')); 
        });
    });

What I want to do, is call this every 3 seconds. 我想做的是每3秒调用一次。 So I thought I'd wrap my getJson call in a setInterval function: 所以我想将getJson调用包装在setInterval函数中:

setInterval(function(){
    // get the JSON from the API
    $.getJSON( "helpers.php", {
        func: "results",
        days: "3",
    })
    .done(function( rows ) {
        $.each(rows, function() {
            // populate the table
            var r = new Array(), j = -1;
            var matchDate = this.date;
                r[++j] = '<tr>';
                r[++j] = '<td><small>' + this.teamname; + '</small></td>';
                r[++j] = '<td>' + this.teamscore; + '</td>';
                r[++j] = '</tr>';
            $('#table-results').append(r.join('')); 
        });
    });
},3000);

However, this obviously carries on 'appending' the rows. 但是,这显然会“附加”行。 I want it to act like a table refresh, so it would essentially start the table again. 我希望它像表刷新一样工作,因此它实际上将再次启动表。 How could I do this if I dont necessarily know the length of the table? 如果我不一定知道桌子的长度,该怎么办?

First you should consider using setTimeout instead of setInterval here and just call it again in your success method. 首先,您应该考虑在此处使用setTimeout而不是setInterval ,并在成功方法中再次调用它。 If your request lasts longer than the interval, the request is executed again before the first one has finished, which could produce unexpected results. 如果您的请求的持续时间长于该间隔,则在第一个请求完成之前会再次执行该请求,这可能会产生意外的结果。 I like the article from JavaScript Garden about this topic. 我喜欢JavaScript Garden中有关该主题的文章

To refresh the table without flickering you should empty the table just before repopulating it in your success method. 要刷新表而不闪烁,您应该在成功方法中重新填充表之前清空表。 The smoothest way would probably be if you assign IDs to the rows and really only update the rows instead of the whole table. 最流畅的方法可能是将ID分配给行,而实际上仅更新行而不是整个表。

Here is an example: 这是一个例子:

function updateTable() {
    $.getJSON("helpers.php", {
        func: "results",
        days: "3",
    }).done(function(rows) {
        // Clear table (only if you have to recreate the whole table)
        // The code below just updates the rows, so this isn't needed.
        //$('#table-results').empty();

        $.each(rows, function() {
            var rowID = this.date; // Use an appropriate ID
            var existingRow = $('#' + rowID);

            // If row does already exist, update it; otherwise create a new one.
            if(existingRow.length > 0) {
                existingRow.find('.teamname').html(this.teamname);
                existingRow.find('.teamscore').html(this.teamscore);
            } else {
                $('#table-results').append([
                    '<tr id="', rowID, '">',
                        '<td><small class="teamname">', this.teamname, '</small></td>',
                        '<td class="teamscore">', this.teamscore, '</td>',
                    '</tr>'
                ].join(''));
            }
        });

        setTimeout(updateTable, 3000);
    });
}

updateTable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM