【问题标题】:javascript setTimeout stops working after including createElementjavascript setTimeout 在包含 createElement 后停止工作
【发布时间】:2013-04-24 13:41:00
【问题描述】:

我正在尝试在我的网站上添加新的幻灯片。除了“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);

我试图通过先加载幻灯片然后调用函数、添加点击激活文本、调用多个函数等来欺骗它。

【问题讨论】:

  • 为什么您认为您需要创建一个<script> 元素,向其中添加代码,并将其附加到某处?只需执行代码。
  • ..天啊...,我不知道从哪里开始...
  • 为什么不用interval而不是timeout呢?或者事件更好,为什么不 window.onresize?只需添加一个油门,这样它就不会调用一千次。
  • 如果您不将要评估的字符串传递给window.setTimeout,而是传递函数本身,问题是否仍然存在; window.setTimeout(getDocSpecs, 2000);?
  • 为什么不用jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: calcheight+'px', background: '#000000'});替换你的第一个sn-p?

标签: javascript jquery settimeout innerhtml createelement


【解决方案1】:

两者不应该相互影响,但同时,你不应该使用createElement 来做你想做的事情。

我稍微整理了一下,将位分隔成清晰的函数并删除了createElement 部分。希望您现在能够更轻松地进行调试。不过,我已尝试保持相同的行为。
正如 cmets 中提到的,您还可以更改为使用事件侦听器来调整 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;
}());

【讨论】:

  • 就像你说的,两者不应该相互影响。我换了另一个幻灯片制作器,脚本现在可以正常工作了。您得到正确答案的原因是因为您提供了一些解决方案;两者不应该相互影响。这将导致其他有同样问题的人尝试使用不同的幻灯片制作工具。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2014-08-24
相关资源
最近更新 更多