简单的 Javascript XML 幻灯片

发布于 2024-11-05 17:18:48 字数 1116 浏览 2 评论 0原文

我似乎无法弄清楚为什么这不起作用。我只有两个简单的函数:一个将图像加载到 img 节点中,另一个加上 i 变量。第二个函数在计时器事件+计数后触发,然后再次触发 image() 函数。除非它不起作用。

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    //XML Loaded, create the slideshow
    alert(xmlhttp.responseText);
    var slideShow = document.getElementById('slideShow');
    var items = [];
    var nl = xmlhttp.responseXML.getElementsByTagName('image');
    var i = 0;
    var t;

    function image() {
        var slideShowImg = document.getElementById('slideShowImg');
        var nli = nl.item(i);
        var src = nli.getAttribute('src').toString();
        var width = parseInt(nli.getAttribute('width').toString());
        var height = parseInt(nli.getAttribute('height').toString());

        var imgNode = document.createElement('img');
        imgNode.setAttribute('src', src);
        imgNode.setAttribute('height', height);
        imgNode.setAttribute('width', width);
        slideShowImg.appendChild(imgNode);

        t = setTimeout("nextImage()", 5000);
    }

    function nextImage() {
        i++;
        image();
    }

    image();
}

I can't seem to figure out why this isn't working. I just have two simple functions: one that loads the image into an img node, and one that pluses an i variable. The second function fires after a timer event, + the count, and then fires the image() function again. Except it's not working.

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    //XML Loaded, create the slideshow
    alert(xmlhttp.responseText);
    var slideShow = document.getElementById('slideShow');
    var items = [];
    var nl = xmlhttp.responseXML.getElementsByTagName('image');
    var i = 0;
    var t;

    function image() {
        var slideShowImg = document.getElementById('slideShowImg');
        var nli = nl.item(i);
        var src = nli.getAttribute('src').toString();
        var width = parseInt(nli.getAttribute('width').toString());
        var height = parseInt(nli.getAttribute('height').toString());

        var imgNode = document.createElement('img');
        imgNode.setAttribute('src', src);
        imgNode.setAttribute('height', height);
        imgNode.setAttribute('width', width);
        slideShowImg.appendChild(imgNode);

        t = setTimeout("nextImage()", 5000);
    }

    function nextImage() {
        i++;
        image();
    }

    image();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-11-12 17:18:48

这行看起来很可疑:

t = setTimeout("nextImage()", 5000);

试试这个:

t = setTimeout(nextImage, 5000);

This line looks very suspicious:

t = setTimeout("nextImage()", 5000);

Try this instead:

t = setTimeout(nextImage, 5000);
千紇 2024-11-12 17:18:48

将函数定义移出 if 语句。

Move the function definitions out of the if statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文