简体   繁体   中英

If Statement to run function before document.ready from Fancybox 'onStart'

I am trying create a function that is called on load of a fancybox from the 'onStart' option. The code will need to check to see if the page has been loading for more than 7 seconds but not completed. If the fancybox has not completed loading within 7 seconds, this function will kill the Fancybox modal. Below is where I am currently but I have gotten too far and am believing it cannot be done.

    $.fancybox({
            'autoScale': false,
            'centerOnScroll': false,
            'enableEscapeButton': false,
            'hideOnOverlayClick': false,
            'href': "link Removed",
            'showCloseButton': false,
            'onStart': function() {
                $.fancybox.showActivity();
                window.onload = function() {
                    var a = new Date();//get seconds of loading start
                }
                var b = new Date();//get seconds after loading started
                var difference = (b - a) / 1000;

                if (document.readyState !== "complete" && difference >= 7) {
                    parent.$.fancybox.close();
                }
            },
            'onComplete': function() {
                $('#fancybox-frame').load(function() { // wait for frame to load and then gets it's height
                    $('#fancybox-content').height($(this).contents().find('body').height() + 30);
                });
                $.fancybox.hideActivity();
            },
            'type': 'iframe'

        });

Looks like a scoping issue..

 window.onload = function() {
     var a = new Date();   // Declared the variable inside the function
 }

  var difference = (b - a) / 1000;
                        ^------- Cannot access it outside the scope

So it will only be available in this context

Try this

var a;

window.onload = function () {
    a = new Date(); //get seconds of loading start
}

$(function () {
    $.fancybox({
        'autoScale': false,
            'centerOnScroll': false,
            'enableEscapeButton': false,
            'hideOnOverlayClick': false,
            'href': "link Removed",
            'showCloseButton': false,
            'onStart': function () {
            $.fancybox.showActivity();
            var b = new Date(); //get seconds after loading started
            var difference = (b - a) / 1000;

            if (document.readyState !== "complete" && difference >= 7) {
                parent.$.fancybox.close();
            }
        },
            'onComplete': function () {
            $('#fancybox-frame').load(function () { 
                      // wait for frame to load and then gets it's height
                $('#fancybox-content').height($(this).contents()
                                      .find('body').height() + 30);
            });
            $.fancybox.hideActivity();
        },
            'type': 'iframe'
    });

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