开发者

Simple Javascript XML Slideshow

开发者 https://www.devze.com 2023-03-04 21:15 出处:网络
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 t

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(xml开发者_开发技巧http.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();
}


This line looks very suspicious:

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

Try this instead:

t = setTimeout(nextImage, 5000);


Move the function definitions out of the if statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消