0

I have a script that uses AJAX/PHP/SQL to query data and pushes it into an array with a bunch of IF's statements. The changeData function is called every 6 seconds. The first query I return I have 6 arrays. The second time i send a request, my push array(IsVacant1) is double and went to 12. after a while, I have over 500 arrays going into my .each statement.

How do I 'clear' this every time I make a request so that I am not adding arrays? Any help is most appreciated.

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();
5
  • So your question is how to empty an Array? Set the length to 0. If instead you wish to remove n items from the beginning of an Array, you can use splice, arr.splice(0, n) Commented Sep 1, 2015 at 16:38
  • Thank you, still trying to find out where to actually pa.length to zero. Commented Sep 1, 2015 at 16:45
  • do you mean something like temp.splice(0,n)={stuff, stuff,}; ????? Commented Sep 1, 2015 at 16:50
  • A really easy way to empty your array: pa = []; Commented Sep 1, 2015 at 16:56
  • @dave the 'pa' doesnt have the previous rows; its the isVacant1.. Need to find the location where to put this to zero. Every where i put it, it does set it to zero and still adds the array... any ideas? Commented Sep 1, 2015 at 17:03

1 Answer 1

2

From your question it looks like you want to clear the isVacant1 array. In your ajax callback just put isVacant1 = []; as the first line. Like this

       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 = '';

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

From your code it's not clear how you are declaring/initializing isVacant1 so i have suggested isVacant1 = [] otherwise you can also use isVacant1.length = 0.

You can also take a look here How do I empty an array in JavaScript?

Sign up to request clarification or add additional context in comments.

2 Comments

This was successful. Thank you .. I declared it as var isVacant1=[]; so I used isVacant1=[]; in the location you specified and it worked.
In that case you can use isVacant1.length = 0; instead of isVacant1 = []; which might be more safer depending on your situation. For more details regarding this please go through the link mentioned in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.