1

After installing this module I get an error sometimes: Images won't load and the next error will show on console: Uncaught TypeError: $(...).lazy is not a function With a link to this section of code in the scripts.phtml

require(['jquery', 'lazyLoad', 'lazyLoadPlugins'], function($) {
    window.mgzLazyload = function(config) {
        var settings = {
            bind: "event",
            delay: <?= $delay ? $delay : '-1' ?>,
            threshold: <?= $threshold ?>,
            effect: "<?= $effect ?>",
            effectTime: <?= $effectTime ?>,
            afterLoad: function(element) {
                $(element).removeClass('lazy-loading').removeClass('lazy-blur').addClass('lazy-loaded');
            }
        };
        if (config) settings = $.extend(settings, config);
        $('.lazy').lazy(settings);
    }
    window.mgzLazyload();
    $(document).on('ajaxComplete', function (event, xhr, settings) {
        window.mgzLazyload();
    });
    $(document).ready(function($) {
        $('.owl-carousel').on('changed.owl.carousel', function(event) {
            window.mgzLazyload();
        });
    });

I tried to fix it on my own, but can't seem to force the module to load lazy before this section. Here is an example: https://jobbooz.com/hombres/indumentaria/todos-los-modelos.html?sale=1&size=168

NOTE: It's not always, most of the time it will load correctly, so you have to load it a couple of times.

2 Answers 2

2

May be the issue the lazyload function gets called before initialization of jquery.

Try $(window).load() method.

1
  • That broke the execution because of the $() function being outside the require(), so it would try to run automatically without waiting for jquery. What I did was adding what its on my answer. Commented Oct 15, 2020 at 2:32
0

What I did was following Hamendra advice but calling the function again at the end basically, not before:

require(['jquery', 'lazyLoad', 'lazyLoadPlugins'], function($) {
    ...
    $(document).ready(function($) {
        $('.owl-carousel').on('changed.owl.carousel', function(event) {
            window.mgzLazyload();
        });
    });
    $(window).load(function() {
    
        window.mgzLazyload();
    });
});

I think this just makes it try once more before giving up. This didn't fix the error 100%, but it go from happening 1 every 4 times to 1 in 20. That's why I don't consider it fixed.

2
  • Is it not fixed yet ? Commented Oct 15, 2020 at 4:43
  • It's working a lot better, a lot, but it will fail from time to time Commented Oct 15, 2020 at 15:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.