如何清空脚本中的数组

时间:2015-09-01 16:32:59

标签: javascript jquery arrays performance

我有一个脚本,它使用AJAX / PHP / SQL查询数据并将其推送到包含一堆IF语句的数组中。每隔6秒调用changeData函数。我返回的第一个查询我有6个数组。第二次发送请求时,我的推送数组(IsVacant1)是双倍的,然后转到12.过了一会儿,我有超过500个数组进入我的.each语句。

每次发出请求时如何“清除”这个以便我不添加数组?任何帮助都非常感谢。

function changeData() {
        isPaused = true;


        var mydata0 = null;
        $.post('php/ProductionChange.php', {
                    'WC': cc
                }, function(data) { // This is Where I use an AJAX call into a php file.

                    mydata0 = data; // This takes the array from the call and puts it into a variable 
                    var pa = JSON.parse(mydata0); // This parses the data into arrays and elements  
                    var temp = {};
                    var bayData = '';

                    if (pa != null) {

                        for (var i = 0; i <= pa.length - 1; i++) {

                            var job = pa[i][0];
                            var shipdate = pa[i][1];
                            var status = pa[i][2];
                            var name = pa[i][3];
                            var EnclLoc = pa[i][13];
                            var Enclsize = pa[i][14];
                            var backpan = pa[i][15];
                            var percentCom = pa[i][16];
                            var IsVisible = pa[i][17];
                            var png = pa[i][18];
                            var WorkC = pa[i][20];
                            baydata = 'bayData' + i + '';
                            temp = {
                                job, shipdate, name, EnclLoc, Enclsize, backpan, percentCom, IsVisible, png, WorkC, status
                            };
                            isVacant1.push({
                                baydata: temp
                            });   
                        }
                    } else {
                        ii = 1;
                        //alert("There are no more job numbers in this bay location. Thank you. ");
                    }
                    $.each(isVacant1, function(key, value) {

                                var job = value.baydata.job;
                                var ship = value.baydata.shipdate;
                                var name = value.baydata.name;
                                var encl = value.baydata.EnclLoc;
                                var EnclSize = value.baydata.EnclLoc;
                                var percentCom = value.baydata.percentCom;
                                var backpan = value.baydata.backpan;
                                var PngLogo = value.baydata.png;
                                var IsVisible = value.baydata.IsVisible;
                                var WorkC = value.baydata.WorkC;
                                var status = value.baydata.status;
                                var p = WorkC;
                                WorkC = (WorkC < 10) ? ("0" + WorkC) : WorkC;

                                //// remember  if the encl location matches the workcell cell then do stuff based on that....... hint encl image not hiding becase of duplicate 17s

                                if (((encl == p) || (backpan == p)) && job != 123) {
                                    $('#WC' + p).show();
                                    document.getElementById("bayData" + p).innerHTML = name + ' ' + ship; // Work Cell  Name and Ship Date
                                    document.getElementById("bayData" + p + "a").innerHTML = job; // Work cell Job Number
                                    document.getElementById("percentCom" + p).innerHTML = percentCom + '%'; // Work Cell Percent Complete                       
                                } else {
                                    $('#WC' + p).hide();

1 个答案:

答案 0 :(得分:2)

从您的问题看起来您想要清除isVacant1数组。 在ajax callback中,只需将isVacant1 = [];作为第一行。喜欢这个

       function(data) { // This is Where I use an AJAX call into a php file.
                isVacant1 = [];

                mydata0 = data; // This takes the array from the call and puts it into a variable 
                var pa = JSON.parse(mydata0); // This parses the data into arrays and elements  
                var temp = {};
                var bayData = '';

                ..................

从您的代码中,您不清楚如何宣布/初始化isVacant1,因此我建议isVacant1 = [],否则您也可以使用isVacant1.length = 0

您还可以在此处查看How do I empty an array in JavaScript?