Neatest (and fast) way to remove top lines from a textarea

前端 未结 2 1543
情深已故
情深已故 2021-01-20 23:46

I have a webpage that displays last 1000 lines of a logfile then updates via AJAX every x seconds loading new content (if any) and appending to textare

2条回答
  •  隐瞒了意图╮
    2021-01-21 00:22

    You should be able to use a single split and then join after truncating the data, something like this:

    // on data retrieved
    var total = ((textarea.value 
                  ? textarea.value + "\n" 
                  : "") 
              + new_data).split("\n");
    
    if (total.length > 10) 
        total = total.slice(total.length - 10);
    
    textarea.value = total.join("\n");
    

    Working example: http://jsfiddle.net/ArvQ7/ (cut to 10 lines for brevity)

提交回复
热议问题