如果要在Fancybox'onStart'上的document.ready之前运行Statement函数

时间:2013-06-03 22:06:22

标签: javascript jquery fancybox document-ready

我正在尝试创建一个从'onStart'选项加载fancybox时调用的函数。 代码需要检查页面是否已加载超过7秒但未完成。如果fancybox未在7秒内完成加载,此功能将终止Fancybox模式。下面是我目前的地方,但我已经走得太远,我相信它无法完成。

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

        });

1 个答案:

答案 0 :(得分: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'
    });