[英]javascript setTimeout stops working after including createElement
我正在尝试在我的网站上合并一个新的幻灯片放映。 除了“ height =“ 80%””的选项外,幻灯片具有我需要的所有东西,即我希望幻灯片与浏览器一起缩放,因为新的网站设计将类似于android应用程序。 完全身临其境。
因为幻灯片本身不具有此选项,所以我创建了一个JavaScript代码,它将每2秒检查一次文档/浏览器窗口的大小,并重新调整幻灯片本身的大小/大小,使其始终适合屏幕大小。 但是,问题在于,在我将某些代码字符串粘贴到脚本中之后,javascript将仅运行一次,onload并且不会调用“ setTimeout”。
因此,问题在于setTimeout实际上停止工作,因此在包含以下代码字符串之后,它就已经工作了:
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
完整的javascript检查功能在这里:
function getDocSpecs() {
clearTimeout(t);
var D = Math.max(
Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
Math.max(document.body.clientHeight, document.documentElement.clientHeight));
var Le = Math.max(
Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
Math.max(document.body.clientWidth, document.documentElement.clientWidth));
calcheight = (0.80 * D);
alert(preheight + "_" + prewidth + "_" + D + "_" + Le + "_");
if (preheight != D || prewidth != Le) {
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML = "jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '" + calcheight + "px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
}
preheight = D;
prewidth = Le;
t = setTimeout('getDocSpecs()', 2000);
}
这两个似乎互不相同:
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
和
t = setTimeout('getDocSpecs()', 2000);
我试图通过先加载幻灯片然后调用该函数,添加单击激活的文本,调用多个函数等来欺骗它。
两者不应该相互影响,但是同时,您不需要为要执行的操作使用createElement
。
我将它整理了一下,将各个部分分成了清晰的函数,并删除了createElement
部分。 希望您现在可以更轻松地进行调试。 不过,我尝试过保持行为不变。
如评论中所述,您还可以更改为使用事件侦听器进行resize ,这将使函数不必被频繁调用。
var getDocSpecs = (function () {
var t,
pre = {h: -1, w: -1},
getDocSpecs;
function asyncFlashGallery(p1, p2, p3) {
return window.setTimeout(function () {jQuery.flashgallery(p1, p2, p3)}, 0);
}
function dMax(nx) {
return Math.max(document.body[nx] || 0, document.documentElement[nx] || 0);
}
getDocSpecs = function getDocSpecs() {
window.clearTimeout(t);
var D = Math.max(
dMax('scrollHeight'), dMax('offsetHeight'), dMax('clientHeight')
),
Le = Math.max(
dMax('scrollWidth'), dMax('offsetWidth'), dMax('clientWidth')
),
calcheight = (0.80 * D);
alert(pre.h + "_" + pre.w + "_" + D + "_" + Le + "_"); // consider console.log
if (pre.h !== D || pre.w !== Le) {
asyncFlashGallery(
'gallery/ArtGallery.swf',
'gallery/gallery.xml',
"{width: '100%', height: '" + calcheight + "px', background: '#000000'}"
);
}
pre.h = D;
pre.w = Le;
t = window.setTimeout(getDocSpecs, 2000);
};
getDocSpecs.CANCEL = function () {window.clearTimeout(t);}
return getDocSpecs;
}());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.