Reputation: 381
function removeTds() {
var elements = $('#goldBarList tr:not(:eq(0))').filter(':has(:checkbox:checked):lt(50)');
var count = elements.length;
elements.each(function() {
grossWeightTotal = grossWeightTotal - $('#goldBarList tr:eq(' + $(this).index() + ') td:eq(8)').text();
netWeightTotal = netWeightTotal - $('#goldBarList tr:eq(' + $(this).index() + ') td:eq(9)').text();
fineOunceTotal = fineOunceTotal - $('#goldBarList tr:eq(' + $(this).index() + ') td:eq(10)').text();
});
elements.remove();
if(count === 50) window.setTimeout(removeTds, 1);
}
removeTds();
The above code results in the "stop running this script?" prompt. I have 4000 records.
Upvotes: 1
Views: 97
Reputation: 382150
Your code is inherently slow and optimizable (cache $(this).index()
for example) but the biggest problem is that you ask the browser to run the function every 1 ms if you have 50*N records (for example 4000...).
Simply clean all in one go.
For example :
function removeTds() {
var elements = $('#goldBarList tr:not(:eq(0))').filter(':has(:checkbox:checked)');
elements.each(function() {
var index = $(this).index();
grossWeightTotal = grossWeightTotal - $('#goldBarList tr:eq(' + index + ') td:eq(8)').text();
netWeightTotal = netWeightTotal - $('#goldBarList tr:eq(' + index + ') td:eq(9)').text();
fineOunceTotal = fineOunceTotal - $('#goldBarList tr:eq(' + index + ') td:eq(10)').text();
});
elements.remove();
}
removeTds();
If you want to have the clean been remade when checkbox are changed, add this :
$('input[type="checkbox"]').change(removeTds);
Of course you may also bind to a button :
$('#removeButton').change(removeTds);
Upvotes: 8