[英]If Statement to run function before document.ready from Fancybox 'onStart'
我正在尝试创建一个从'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'
});
看起来像是一个范围问题。
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'
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.