0

In a $.each function I am calling jquery ajax method which is returning a list, so that returned list I want to use in other function and parallel that jquery ajax loop will also work and return the value continuously and call that other function, but it's not happening parallel,means after completing of $.each function it's calling the other function with the last returned list. But I want once its returned list call the other function with that returned value and like this continue.

function Test() { 
            var x = $('#myHiddenVar').val(); 
            $.each($.parseJSON(x), function (key, value) { 
                     $.ajax({
                        type: 'POST',
                        url: '@Url.Action("GeoIPDashboardDataload", "Home")',
                        data: { branchId: value },
                        async: false, 
                        error: function (xhr, status, error) {
                            console.log(error);
                        },
                        success: function (data) {
                            debugger;                            
                            if (data != null) { 
                                $(data.logs).each(function (index) {
                                    debugger;
                                    var _id = index;
                                    var _logId = data.logs[index].id;
                                    var _dstIP = data.logs[index].dst_ip;
                                    var _dstCountryName = data.logs[index].dst_geoData.country_name;
                                    var _dstlatitude = data.logs[index].dst_geoData.latitude;
                                    var _dstlongitude = data.logs[index].dst_geoData.longitude;
                                    var _srcIP = data.logs[index].src_ip;
                                    var _srcCountryName = data.logs[index].src_geoData.country_name;
                                    var _srclatitude = data.logs[index].src_geoData.latitude;
                                    var _srclongitude = data.logs[index].src_geoData.longitude;
                                    var _threatType = data.logs[index].threat_type;

                                    var locations = {
                                        "id": _id,
                                        "logId": _logId,
                                        "dstIP": _dstIP,
                                        "dstCountryName": _dstCountryName,
                                        "dstlatitude": _dstlatitude,
                                        "dstlongitude": _dstlongitude,
                                        "srcIP": _srcIP,
                                        "srcCountryName": _srcCountryName,
                                        "srclatitude": _srclatitude,
                                        "srclongitude": _srclongitude,
                                        "threatType":_threatType
                                };
                                    queryStr = { "locations": locations }; 
                                queryArr.push(queryStr);
                                });
                                companyName = data.comp;
                                branchName = data.branch; 
                                attacks.init();
                            }

                        }                       
                    }); 
            });
        }

------------


attacks = {

                interval: getRandomInt(attack_min, attack_max),

                init: function () {                    
                    setTimeout(
                        jQuery.proxy(this.getData, this),
                        this.interval
                   );
                },

                getData: function () {
                    var srclat = arr[arrayCount].src_geoData.latitude;
                    var srclong = arr[arrayCount].src_geoData.longitude; 
                    var dstlat = arr[arrayCount].dst_geoData.latitude;
                    var dstlong = arr[arrayCount].dst_geoData.longitude;                   
                    var sourceip = arr[arrayCount].src_ip;
                    var attackerIP = arr[arrayCount].dst_ip;
                    var srccountry = arr[arrayCount].src_geoData.country_name;
                    attackdiv_slatlong = arr[arrayCount].dst_geoData.country_name;

                    hits.push({
                        origin: { latitude: srclat, longitude: srclong },
                        destination: { latitude: dstlat, longitude: dstlong }
                    });
                    map.arc(hits, { strokeWidth: 2 }); 
                    boom.push({
                        radius: 7, latitude: dstlat, longitude: dstlong,
                        attk: attackerIP
                    }); 
                    map.bubbles(boom, {
                        popupTemplate: function (geo, data) {
                            return '<div class="hoverinfo">' + data.attk +'</div>';
                        }
                    }); 
                    arrayCount++; 
                    this.interval = getRandomInt(attack_min, attack_max);
                    this.init();                   
                },
            }; 

So the above test method calling from document.ready function and after pushing all item into the queryArr array call attack.init and inside attack.int i am using that array list.

Shibu
  • 102
  • 9
  • If you want it to work asynchronous why do you put `async: false,` ? – Romain B. Feb 01 '19 at 08:56
  • @RomainB, I tried with after comment of that line `async: false` but still the same problem I am facing. – Shibu Feb 01 '19 at 08:59
  • you need to use `deferred` concept. – Vel Feb 01 '19 at 09:03
  • https://stackoverflow.com/questions/37047280/deferred-and-ajax – Vel Feb 01 '19 at 09:05
  • `async: false` is deprecated and should not be used. It gives very poor user experience, and anyway since it's deprecated you can expect that browsers will stop supporting it in the future. Anyway I'm fairly sure your issue may be a scoping one, since all iterations of the loop are trying to use the same global `attacks` variable, it will just keep overwriting the values. You need to create new instances of your object I think. – ADyson Feb 01 '19 at 10:16

0 Answers0