0

我正在尝试创建一个在从“onStart”选项加载fancybox时调用的函数。代码将需要检查页面是否已加载超过 7 秒但未完成。如果fancybox在7秒内没有完成加载,这个函数会杀死Fancybox modal。下面是我目前所处的位置,但我已经走得太远了,我相信它无法完成。

    $.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'

        });
4

1 回答 1

0

看起来像一个范围问题..

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

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

所以它只会在这种情况下可用

尝试这个

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'
    });
于 2013-06-03T22:11:03.447 回答