[英]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.