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