稍后的新数据将替换 jQuery 中的当前数据

发布于 2024-12-18 17:49:57 字数 1639 浏览 0 评论 0 原文

$(document).ready(function(){
      setInterval(function(){
         $.ajax({ url: "getLiveData.php",
             success: function(result){
                 $.each(result, function(i, result){
                    var t = $("table#insideTable");
                    t.append('<tr><td>' + result.carNo + '</td><td>' +
                             result.carSpeed + '</td></tr>');
                    });
             }, 
             dataType: "json"
          });
      }, 600000);
});

你好,我试图使用上面的代码每 10 分钟更新一次车速。

--10:20AM的数据-----

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |170 kph|
+-------+-------+
|2      |150 kph|
+-------+-------+
|3      |190 kph|
+-------+-------+

--10:30AM的数据-----

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

但是,运行代码后,得到的结果是两个时间点都显示出来,一个接一个(见下文)。

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |170 kph|
+-------+-------+
|2      |150 kph|
+-------+-------+
|3      |190 kph|
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

我真正想要的是用稍后的新数据来替换当前的数据。

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

谁能帮助我吗?

非常感谢!

$(document).ready(function(){
      setInterval(function(){
         $.ajax({ url: "getLiveData.php",
             success: function(result){
                 $.each(result, function(i, result){
                    var t = $("table#insideTable");
                    t.append('<tr><td>' + result.carNo + '</td><td>' +
                             result.carSpeed + '</td></tr>');
                    });
             }, 
             dataType: "json"
          });
      }, 600000);
});

Hello, I was trying to use the code above to update car speeds for every 10 mins.

--data at 10:20AM-----

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |170 kph|
+-------+-------+
|2      |150 kph|
+-------+-------+
|3      |190 kph|
+-------+-------+

--data at 10:30AM-----

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

However, after running the code, the results obtained from the two time points are all shown, with one after the other (See below).

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |170 kph|
+-------+-------+
|2      |150 kph|
+-------+-------+
|3      |190 kph|
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

What I really want is to have the new data from a later time to replace the current one.

+-------+-------+
|car    |speed  |
+-------+-------+
|1      |180 kph|
+-------+-------+
|2      |155 kph|
+-------+-------+
|3      |174 kph|
+-------+-------+

Can anyone help me?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗喜 2024-12-25 17:49:57

您想使用 .html() 函数代替 .append()

更改:

success: function(result){
    $.each(result, function(i, result){
        var t = $("table#insideTable");
        t.append('<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>');
    });
}

到:

success: function(result){

    //create an output variable that will be added to the DOM when we're finished
    var output = '';

    //iterate through the results of the AJAX request
    $.each(result, function(i, result){

        //add a row to the output variable
        output += '<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>';
    });

    //add the output to the DOM, notice that since we are only adding nodes to the DOM once this creates less CPU overhead than appending each row separately (we also save overhead by not selecting the table element during each iteration of the $.each() loop)
    $("#insideTable").html(output);
}

这将替换表元素内的 HTML,而不是附加到它。以下是使用 .html().append() 的演示: http://jsfiddle.net/jasper/TKaVF/

这是 .html() 的文档:http://api.jquery.com/html

顺便说一句,添加 table 并不会让你的选择器更快,因为你正在寻找-up一个ID(这本身就相当快)。

var t = $("table#insideTable");

会更快:

var t = $("#insideTable");

You want to use the .html() function in place of .append():

Change:

success: function(result){
    $.each(result, function(i, result){
        var t = $("table#insideTable");
        t.append('<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>');
    });
}

To:

success: function(result){

    //create an output variable that will be added to the DOM when we're finished
    var output = '';

    //iterate through the results of the AJAX request
    $.each(result, function(i, result){

        //add a row to the output variable
        output += '<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>';
    });

    //add the output to the DOM, notice that since we are only adding nodes to the DOM once this creates less CPU overhead than appending each row separately (we also save overhead by not selecting the table element during each iteration of the $.each() loop)
    $("#insideTable").html(output);
}

Which will replace the HTML inside the table element rather than append to it. Here is a demonstration of using .html() versus .append(): http://jsfiddle.net/jasper/TKaVF/

Here is the documentation for .html(): http://api.jquery.com/html

On a side-note, it doesn't make your selector any faster by adding table since you are looking-up an ID (which is quite fast on its own).

var t = $("table#insideTable");

Would be faster as:

var t = $("#insideTable");
╄→承喏 2024-12-25 17:49:57

工作示例: http://jsfiddle.net/manseuk/mnRTf/

将成功方法更改为:

$('#tablecontainerdiv').html('<table>');
$.each(result, function(i, result){
    $("#tablecontainerdiv table").append('<tr><td>' + result.carNo + '</td><td>' +
              result.carSpeed + '</td></tr>');
});
$('#tablecontainerdiv').append('</table>');

其中 tablecontainerdiv 是包含表格的 div,例如:

<div id="tablecontainerdiv">
  <table>
    //your data here
  </table>
</div>

新代码将用新数据替换旧表格

Working example : http://jsfiddle.net/manseuk/mnRTf/

change to your success method to :

$('#tablecontainerdiv').html('<table>');
$.each(result, function(i, result){
    $("#tablecontainerdiv table").append('<tr><td>' + result.carNo + '</td><td>' +
              result.carSpeed + '</td></tr>');
});
$('#tablecontainerdiv').append('</table>');

where tablecontainerdiv is a div that the table is contained in for example :

<div id="tablecontainerdiv">
  <table>
    //your data here
  </table>
</div>

The new code will replace the old table with the new data

娜些时光,永不杰束 2024-12-25 17:49:57

在添加新行之前清空表:

success: function(result){
             var t = $("table#insideTable"); // moved outside the loop
             t.empty();                      // remove contents of the element
             $.each(result, function(i, result){
                t.append('<tr><td>' + result.carNo + '</td><td>' +
                         result.carSpeed + '</td></tr>');
                });
         } 

Just empty the table before you add the new rows:

success: function(result){
             var t = $("table#insideTable"); // moved outside the loop
             t.empty();                      // remove contents of the element
             $.each(result, function(i, result){
                t.append('<tr><td>' + result.carNo + '</td><td>' +
                         result.carSpeed + '</td></tr>');
                });
         } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文