window.onload = initAll;
var xhr = false;

function initAll() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        getPix();
    }
    else {
        alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
}

function getPix() {
    xhr.open("GET", "flickrfeed.xml", true);
    xhr.onreadystatechange = showPictures;
    xhr.send(null);

    setTimeout(getPix, 5 * 1000);
}

function showPictures() {
    var tempText = document.createElement("div");
            
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var allImages = xhr.responseXML.getElementsByTagName("content");
            var randomImg = Math.floor(Math.random() * allImages.length);

            tempText.innerHTML = getPixVal(allImages[randomImg]);
            var thisImg = tempText.getElementsByTagName("p")[1];
            document.getElementById("pictureBar").innerHTML = thisImg.innerHTML;
        }
        else {
            alert("There was a problem with the request " + xhr.status);
        }
    }
    
    function getPixVal(inVal) {
        return (inVal.textContent) ? inVal.textContent : inVal.text;
    }
}
View Code

相关文章:

  • 2021-07-24
  • 2021-11-14
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-05-29
猜你喜欢
  • 2021-08-07
  • 2021-11-15
  • 2021-09-06
  • 2021-04-20
  • 2021-05-29
  • 2021-07-05
  • 2021-10-19
相关资源
相似解决方案