To answer your question, in general, instead of using a setTimeout
, it is much better to include whatever code you want to run after the ajax has finished (within the .done()
function)
So, in this case, modify this bit of code:
.done(function(distanceMatrixResult) {
distance = distanceMatrixResult[0];
time = distanceMatrixResult[1];
$('#route-'+item.id).find('td:eq(2)').html(distance);
$('#route-'+item.id).find('td:eq(3)').html(time);
// update tablesorter content
$("#advies-content").trigger('update');
});
Make sure not to call the tablesorter function a second time (.tablesorter()
) as it will only cause problems; but instead trigger an update method, shown in the code above.
As a side note, the code that builds your table can be made much more efficient. For one, try to minimize DOM interaction, meaning try to do everything in one append()
:
// create head
var string = '<thead>'+
'<tr">'+
'<th></th>'+
'<th>Icoon</th>'+
'<th>Afstand</th>'+
'<th>Tijd</th>'+
'<th>Opties</th>'+
'</tr>'+
'</thead>'+
'<tbody>';
// create body
$.each(options,function(key,item){
// make tab content
string +=
'<tr id="route-'+item.id+'">';
// image
'<td valign="top"></td>'+
'<td valign="top" align="center"><img src="'+item.image+'"></td>'+
// distance
'<td valign="top">'+
'</td>'+
// time
'<td valign="top">'+
'</td>'+
// distance + time
'<td valign="top">'+
'<h5 class="click-route" mylatlng="'+item.destination+'"><font color="#21bba3">Route bepalen ></font></h5>'+
'<h3>€ '+item.price+' per uur</h3></td>'+
'</td>'+
'</tr>';
});
$('#route-'+item.id)
.append(string + '</tbody>')
.tablesorter();
Also, most browsers have deprecated use of align
& valign
attributes, and <font>
tags, so if you are using HTML5, please keep that in mind.