简体   繁体   中英

Lazy load Google map

I want to Lazy Load Google Maps to get a better pageload-speed for SEO. My javascript works, but it still tries to load the map multiple times. How can i get it to load once? I found other scripts but i want to use as less code as possible.

My script:

google = false;
$(window).scroll(function() {
   var hT = $('#google-map').offset().top,
       hH = $('#google-map').outerHeight(),
       wH = $(window).height(),
       wS = $(this).scrollTop();
   if (wS > (hT+hH-wH)){
        if (!google) $.getScript( 'https://maps.googleapis.com/maps/api/js?key={key}', function( google, textStatus, jqxhr ) {
            initialize(); 
        });   
   }
});

Console log:

js?key={key}&_=1591085205888:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205888:140
js?key={key}&_=1591085205890:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205890:140
js?key={key}&_=1591085205891:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
dj @ js?key={key}&_=1591085205891:140 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.

Thanks in advance! Peter

I think you are running the script over and over? Add a google = true after the if statement?

google = false;
$(window).scroll(function() {
   var hT = $('#google-map').offset().top,
       hH = $('#google-map').outerHeight(),
       wH = $(window).height(),
       wS = $(this).scrollTop();
   if (wS > (hT+hH-wH)){
        if (!google) {
           google = true; // stop the check
           // enter your loading script here, use debounce and promise for async.
        }
   }
});

Revised as per suggestion from musefan You can also make it like

    var $googleMap = $('#google-map'); // use a ref
    var $window = $(window); // use a ref
    var google = false;

$(window).scroll(function() {
    var hT = $googleMap.offset().top,
        hH = $googleMap.outerHeight(),
        wH = $window.height(),
        wS = $window.scrollTop();
    if (wS > (hT+hH-wH)){
         // if you do the unregister approach, you dont even need to check for google var
        if (!google) { 
            google = true; // stop the check         
            // enter your loading script here, use debounce and promise for async.
        // unregister your event as musefan said
                    }
               }
            });

This works, only fires once. Thanks Sprep && Musefan

    var fired = false;
    $(window).scroll(function() {
       var hT = $('#google-map').offset().top,
           hH = $('#google-map').outerHeight(),
           wH = $(window).height(),
           wS = $(this).scrollTop();
           wB = hT+hH-wH;
        if (wS > wB  && fired === false ){
            console.log('This will happen only once'); 
            fired = true;
            $.getScript( 'https://maps.googleapis.com/maps/api/js?key={key}', function( google, textStatus, jqxhr ) {
                initialize(); 
            });   
       }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM