简体   繁体   English

javascript setTimeout在包含createElement之后停止工作

[英]javascript setTimeout stops working after including createElement

I'm trying to incorporate a new slideshow on my site. 我正在尝试在我的网站上合并一个新的幻灯片放映。 The slideshow has everything I need except the option for "height = "80%"", ie I want the slideshow to scale with the browser because the new site design will sort of by like an android application; 除了“ height =“ 80%””的选项外,幻灯片具有我需要的所有东西,即我希望幻灯片与浏览器一起缩放,因为新的网站设计将类似于android应用程序。 fully immersive. 完全身临其境。

Because the slideshow itself doesn't have this option, I'm creating a javascript code that will check the document/browser window size every 2 seconds and reload/resize the slideshow itself so that it always fits the screen. 因为幻灯片本身不具有此选项,所以我创建了一个JavaScript代码,它将每2秒检查一次文档/浏览器窗口的大小,并重新调整幻灯片本身的大小/大小,使其始终适合屏幕大小。 But, the problem is that the javascript will only run once, onload, and doesn't call "setTimeout" after I paste a certain string of code into the script. 但是,问题在于,在我将某些代码字符串粘贴到脚本中之后,javascript将仅运行一次,onload并且不会调用“ setTimeout”。

So, the problem is that setTimeout actually STOPS working, so it has worked before, after I include this string of code: 因此,问题在于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);

The full javascript check function is here: 完整的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);
}

These two seem to not like each other: 这两个似乎互不相同:

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);

and

t = setTimeout('getDocSpecs()', 2000);

I've tried to trick it by loading the slideshow first then calling the function, adding a click activated text, calling multiple functions, etc. 我试图通过先加载幻灯片然后调用该函数,添加单击激活的文本,调用多个函数等来欺骗它。

The two shouldn't effect eachother, but at the same time, you shouldn't need to use createElement for what you're trying to do. 两者不应该相互影响,但是同时,您不需要为要执行的操作使用createElement

I've tidied it up a little, separated bits into clear functions and removed the createElement parts. 我将它整理了一下,将各个部分分成了清晰的函数,并删除了createElement部分。 Hopefully you'll be able to debug more easily now. 希望您现在可以更轻松地进行调试。 I've tried to keep the behaviour the same otherwise, though. 不过,我尝试过保持行为不变。
As mentioned in comments, you could also change to using an event listener for resize , which will save the function from having to be called so often. 如评论中所述,您还可以更改为使用事件侦听器进行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM