简体   繁体   English

如何提高性能..?

[英]How to improve performance..?

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. 我有4000条记录。

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...). 您的代码本质上是缓慢且可优化的(例如,缓存$(this).index() ),但是最大的问题是,如果您有50 * N条记录(例如4000条记录),则要求浏览器每1毫秒运行一次该函数。 )。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM