I am trying to print array in jquery, I want it to run every 4 sec and want to clear data written in previous loop before it print out again after 4 second. but it is adding the data(rows) rather than replacing.
function sendRequest(){
$.ajax({
type: 'POST',
url:'<?php echo site_url('ajax/ajax_buybtc'); ?>',
success:
function(result){
var jsonResult = JSON.parse(result);
jsonResult.forEach(function(data) {
var newTr = "<tr>";
newTr += '<tr> <td style="text-align:center;"><a><span class="label label-
warning">Sell</span></a></td>';
newTr += "<td>" + data.xbtc + "</td>";
newTr += "<td>" + data.rate + "</td>";
newTr += "<td>" + data.xpkr + "</td>";
newTr += "</tr>";
$('.table > tbody:last-child').append(newTr);
});
setTimeout(function(){
sendRequest(); //this will send request again and again;
}, 4000);
}
});
}
Here is the HTML part
<table class="table table-striped table-condensed" style=" width:100%; float:left; border:3px solid white; box-shadow: 2px 2px 2px white">
<tbody>
<tr>
<td style="text-align:center;"><b>Action</b></td>
<td style="text-align:center;"><b>BTC</b></td>
<td style="text-align:center;"><b>BID</b></td>
<td style="text-align:center;"><b>PKR</b></td>
</tr>
<tr class="messages">
<td style="text-align:center;"><a><span class="label label-warning">Sell</span></a></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
</tr>
</tbody>
</table>

