how to optimize a request made in ajax using jquery in a json file

Asked

Viewed 265 times

0

I have this question: how to optimize my ajax requests in jquery?

I have an application that uses the google maps api, and in this to show the bookmarks on the screen I use jquery to perform the requests in ajax, follow my request code:

function toggleLayers(indexCamada, check)
                {

                    if (check.checked) {
                        //$.get('arquivo.asp')
                        $.ajax({
                            type: "GET",
                            dataType: "json",
                            timeout: 20000,
                            url: "camadas_nivel1.json",
                            contentType: "application/json;charset=UTF-8",
                            cache: true
                        }).done(function(camadas, textStatus, jqXHR) {
                            $.each(camadas, function(index, camada) {

                                if (indexCamada == camada.index) {

                                    $.each(camada.coordenadas, function(index, coordenada) {
                                        var marker = new google.maps.Marker({
                                            position: new google.maps.LatLng(coordenada.latitude, coordenada.longitude),
                                            title: coordenada.title,
                                            map: map,
                                        });
                                        if (coordenada.icon) {
                                            marker.setIcon(coordenada.icon);
                                        }
                                        var c = coordenada['info_window'] ? coordenada['info_window'].join("\n") : "";

                                        if (c !== "") {
                                            var infowindow = new google.maps.InfoWindow({
                                                content: c
                                            });
                                            google.maps.event.addListener(marker, 'click', function() {
                                                infowindow.open(map, marker);
                                                $('.single-item').slick({
                                                    lazyLoad: 'ondemand',
                                                    slidesToShow: 1,
                                                    slidesToScroll: 1
                                                });
                                            });
                                        }
                                        markers.push([camada.index,marker]);

                                        console.log(indexCamada);

                                    });
                                }
                            });
                        }).fail(function(camadas, textStatus, jqXHR) {
                            console.log(camadas + " " + textStatus);
                        })


                    } else {

                        for (i = markers.length - 1; i >= 0; i--) {

                            var indexTmp = markers[i][0];                                
                            var marker = markers[i][1];

                            if (indexCamada == indexTmp ) {
                                marker.setMap(null);
                                markers.splice(i, 1);
                            }
                        }
                        console.log(markers);

                    }
                }

Let’s imagine the screen you have google maps map loaded left in a div and several checks to the right of the screen for user selection, as it selects a check is shown the markers, then the face script checks if the check was checked if yes I do the ajax request in the json file, or I remove the markers if case was rendered on the screen. My questions are:

1 - how do I optimize my requests with large amounts of bookmarks for example 10,000 and etc, there is performance loss I loading whole and scanning entire file using jquery in this way?

2 - will your pass parameters in the requisicao for a *.Asp file , and this build the json file dynamically for specific request dimmed the query time and scan, example:

  $.getJSON("arquivo.asp",{id:1},function(camadas){
    //varrendo arquivo
    $.each(camadas,function(index,camada){

    })
  })

That’s it guys I already appreciate if someone help me

  • 1

    Here are some simple tips: Break your code into more functions for easy reading and maintenance. Let small functions for easy understanding document your functions Soon if you need to debug to measure performance "named" functions are easier to identify.

  • Thank you #Luan Fagundes I will review my code and make these adjustments

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.