javascript - 如何在脚本中清空数组

标签 javascript jquery arrays performance

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

每次发出请求时如何“清除”它,这样我就不会添加数组?非常感谢任何帮助。

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

最佳答案

从您的问题来看,您似乎想清除 isVacant1 数组。 在您的 ajax 回调 中,只需将 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?

关于javascript - 如何在脚本中清空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336698/

相关文章:

arrays - 对三个数组的元素进行排序的算法

javascript - 从 API 获取数据并显示为名称和网站链接

jquery - jstree 复选框的节点 id

javascript - 将参数从输入传递到 onclick 方法

javascript - jQuery/Javascript 动画按时间间隔改变 css 值

php - 将MYSQL结果拆分为4个数组

c - 如何将 IplImage 转换为 char 数组

javascript - 如何更改光标图标以表示 Google 图表上的区域是可点击的?

javascript - JS中日期的日期名称

javascript - 如何提供动态更新的视频?