Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 控制使用服务器发送事件的数据写入表的速度_Javascript_Jquery_Html_Server Sent Events - Fatal编程技术网

Javascript 控制使用服务器发送事件的数据写入表的速度

Javascript 控制使用服务器发送事件的数据写入表的速度,javascript,jquery,html,server-sent-events,Javascript,Jquery,Html,Server Sent Events,我正在用html编写一个表,其中包含从服务器发送事件接收到的数据。以下是我的Javascript: $(document).ready( function() { var sse = new EventSource('/my_event_source'); sse.onmessage = function(e) { // Build the table values = e.data.split("\t"); var rows_data

我正在用html编写一个表,其中包含从服务器发送事件接收到的数据。以下是我的Javascript:

$(document).ready(
  function() {
    var sse = new EventSource('/my_event_source');

    sse.onmessage = function(e) {

      // Build the table
      values = e.data.split("\t");
      var rows_data = [];
      $.each(values, function(index, value) {
        rows_data.push("<td>" + value + "</td>")
      });

      var table_row = "<tr>" + rows_data.join() + "</tr>";
      $("#some_div").append(table_row);

    };
})
$(文档)。准备好了吗(
函数(){
var sse=new EventSource(“/my_event_source”);
sse.onmessage=函数(e){
//建表
值=e.data.split(“\t”);
var行_数据=[];
$.each(值、函数(索引、值){
行\数据推送(“+value+”)
});
var table_row=“”+rows_data.join()+”;
$(“#some_div”).append(表格行);
};
})

虽然表一次写入一行,但行的写入速度非常快!有没有关于如何放慢写作速度的建议?JavaScript显然没有睡眠函数,所以我一直在尝试使用setTimeout(),但没有得到想要的结果。我也尝试过jQuery中的delay(),但那是用于动画的。

不是最好的解决方案,但应该可以:

var queue=[];
var interval=setInterval(function(){addRow(), 1000});

function addRow(){
    if(queue.length > 0){
       var row= queue[0];
       queue.shift();
       $("#some_div").append(row);
    }
}

$(document).ready(
function() {
var sse = new EventSource('/my_event_source');

sse.onmessage = function(e) {

  // Build the table
  values = e.data.split("\t");
  var rows_data = [];
  $.each(values, function(index, value) {
    rows_data.push("<td>" + value + "</td>")
  });

  var table_row = "<tr>" + rows_data.join() + "</tr>";
  queue.push(table_row);      


};
var队列=[];
var interval=setInterval(函数(){addRow(),1000});
函数addRow(){
如果(queue.length>0){
变量行=队列[0];
queue.shift();
$(“#some_div”)。追加(行);
}
}
$(文件)。准备好了吗(
函数(){
var sse=new EventSource(“/my_event_source”);
sse.onmessage=函数(e){
//建表
值=e.data.split(“\t”);
var行_数据=[];
$.each(值、函数(索引、值){
行\数据推送(“+value+”)
});
var table_row=“”+rows_data.join()+”;
queue.push(表_行);
};

})

太棒了。一张小纸条
var interval=setInterval(函数(){addRow()},1000)这就是你想要的。