在jquery中,第一个循环结束后调用其他函数

时间:2019-02-01 08:43:25

标签: javascript c# jquery elasticsearch datamaps

在$ .each函数中,我正在调用返回列表的jquery ajax方法,这样我想在其他函数中使用的返回列表将与之并行,jquery ajax循环也将起作用并连续返回值并调用另一个函数,但它不是并行发生的,意味着在完成$ .each函数后,它会使用最后返回的列表调用另一个函数。但是我想一旦它的返回列表用该返回值调用另一个函数,然后像这样继续。

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

因此,上述测试方法是从document.ready函数调用的,并将所有项目推入queryArr数组后,调用Attack.init并在Attack.int内部,我正在使用该数组列表。

0 个答案:

没有答案