为什么这个JavaScript会减慢我的网页速度

时间:2018-04-09 12:21:28

标签: javascript json

我是JavaScript新手,我编写了一个每5秒刷新一次网络时代节点的函数。我加载了一个大小为68字节的json文件,并解析了html更改数据。

大约20秒后,年龄冻结并放慢了浏览器的速度,同样也没有发生班级变化。

我不知道我是否有内存泄漏,或者我应该让服务器发送数据。

setInterval(function() {
  $.getJSON("/Assets/inplay.json", function(data){
        var j_data = JSON.parse(data);
        var Curr = [];
        $.each(j_data, function(key, value){
            Curr.push(value._id.$oid);
            chk = document.getElementById('HT-'+value._id.$oid);
            if (chk == null){
                $('#in-play').append(addRow(value));
            } else {
                var prefixes = ['GT-', 'HG-', 'HDA-', 'HA-', 'HP-', 'HOT-', 'HOffT-', 'HPI-', 'Fav-', 'AG-', 'ADA-', 'AA-', 'AP-', 'AOT-', 'AOffT-', 'API-', 'HMO-', '025-', '035-', '045-', 'AMO-', 'U25-', 'U35-', 'U45-', 'DMO-'];
                var values = [value.Stats[0].gt, value.Stats[0].hg, chkVal(value.Stats[0],'Dangerous_Attacks', 0), chkVal(value.Stats[0], 'Attacks', 0), chkVal(value.Stats[0], 'Possession', 0), chkVal(value.Stats[0], 'On_Target', 0), chkVal(value.Stats[0], 'Off_Target', 0), chkVal2(value.HomePI, 'PI'), chkVal3(value.Fav), value.Stats[0].ag, chkVal(value.Stats[0], 'Dangerous_Attacks', 1), chkVal(value.Stats[0], 'Attacks', 1), chkVal(value.Stats[0], 'Possession', 1), chkVal(value.Stats[0], 'On_Target', 1), chkVal(value.Stats[0], 'Off_Target', 1), chkVal2(value.AwayPI, 'PI'), marketsFunc(value, "MATCH_ODDS", 0), marketsFunc(value, "OVER_UNDER_25", 1), marketsFunc(value, "OVER_UNDER_35", 1), marketsFunc(value, "OVER_UNDER_45", 1), marketsFunc(value, "MATCH_ODDS", 1), marketsFunc(value, "OVER_UNDER_25", 0),  marketsFunc(value, "OVER_UNDER_35", 0),  marketsFunc(value, "OVER_UNDER_45", 0), marketsFunc(value, "MATCH_ODDS", 2)];
                for (var i = 0, len = prefixes.length; i < len; i++) {
                    if (values[i] != null && document.getElementById(prefixes[i]+value._id.$oid) != null) {
                        //console.log(prefixes[i]+value._id.$oid);
                        //console.log(values[i]);

                        if (prefixes[i] == 'GT-' && value.Tactics != null){
                            if(value.Tactics.indexOf("HT-LTD") != -1 && values[i] == '45:00') {
                                class_n = document.getElementById('HT-'+value._id.$oid).classList;
                                class_n.add('HT-LTD');
                                class_n = document.getElementById('AT-'+value._id.$oid).classList;
                                class_n.add('HT-LTD');
                            } else if (classArr.indexOf("HT-LTD") != -1 && values[i] != '45:00') {
                                class_n = document.getElementById('HT-'+value._id.$oid).classList;
                                class_n.remove('HT-LTD');
                                class_n = document.getElementById('AT-'+value._id.$oid).classList;
                                class_n.remove('HT-LTD');
                            }
                        } 
                        if (prefixes[i] == 'HPI-' && values[i] > 6) {
                            class_n = document.getElementById('HT-'+value._id.$oid).classList;
                            class_n.add('HI-PRESSURE');
                        } 
                        if (prefixes[i] == 'API-' && values[i] > 6) {
                            //console.log('bob');
                            class_n = document.getElementById('AT-'+value._id.$oid).classList;
                            class_n.add('HI-PRESSURE');
                        } 
                        if (prefixes[i] == 'HG-' && values[i] != document.getElementById(prefixes[i]+value._id.$oid).innerHTML) {
                            document.getElementById('HG-'+value._id.$oid).className = 'GOAL';
                        } 
                        if (prefixes[i] == 'AG-' && values[i] != document.getElementById(prefixes[i]+value._id.$oid).innerHTML) {
                            document.getElementById('AG-'+value._id.$oid).className = 'GOAL';
                        } 
                        if (prefixes[i] == 'HG-' && document.getElementById('HG-'+value._id.$oid).className == 'GOAL') {
                            document.getElementById('HG-'+value._id.$oid).className = 'OLD-GOAL';
                        } 
                        if (prefixes[i] == 'AG-' && document.getElementById('AG-'+value._id.$oid).className == 'GOAL') {
                            document.getElementById('AG-'+value._id.$oid).className = 'OLD-GOAL';
                        } 
                        if (prefixes[i] == 'HG-' && document.getElementById('HG-'+value._id.$oid).className == 'OLD-GOAL') {
                            document.getElementById('HG-'+value._id.$oid).className = '';
                        } 
                        if (prefixes[i] == 'AG-' && document.getElementById('AG-'+value._id.$oid).className == 'OLD-GOAL') {
                            document.getElementById('AG-'+value._id.$oid).className = '';
                        } 
                        if (prefixes[i] == 'HPI-' && values[i] < 7) {
                            class_n = document.getElementById('HT-'+value._id.$oid).classList;
                            class_n.remove('HI-PRESSURE');
                        } 
                        if (prefixes[i] == 'API-' && values[i] , 7) {
                            class_n = document.getElementById('AT-'+value._id.$oid).classList;
                            class_n.remove('HI-PRESSURE');
                        }
                        //console.log('ya');
                        document.getElementById(prefixes[i]+value._id.$oid).innerHTML = values[i];
                    }

                }
            }
        });
        var myGames = [];
        var cl = '';
        var n = [];
        var table = document.getElementById("in-play");
        for (var i = 0, row; row = table.rows[i]; i++) {
           //iterate through rows
           //rows would be accessed using the "row" variable assigned in the for loop
           if (row.className == 'even') {

                n = row.id.split('-');
                cl = n[n.length - 1];
                myGames.push(cl);
                //console.log(cl);

           }
        }
        //console.log(myGames);
        for(var i = 0; i < myGames.length; i++) {
            if (Curr.indexOf(myGames[i]) === -1 && myGames[i] != ""){
                //console.log(myGames[i]);
                var row = document.getElementById('HT-'+myGames[i]);
                if (row != null) {
                    row.parentNode.removeChild(row);
                }
                row = document.getElementById('AT-'+myGames[i]);
                if (row != null) {
                    row.parentNode.removeChild(row);
                }
                var tr = document.getElementsByClassName('market-'+myGames[i]);
                var table = document.getElementById('in-play');

                for(var i = tr.length - 1; i >= 0; i--){
                    table.deleteRow(tr[i].rowIndex);
                }
                tr = document.getElementsByClassName('form-'+myGames[i]);
                table = document.getElementById('in-play');

                for(var i = tr.length - 1; i >= 0; i--){
                    table.deleteRow(tr[i].rowIndex);
                }
            }
        }

    });
}, 5000);

感谢您的时间。

0 个答案:

没有答案