我似乎無法弄清楚爲什麼這不起作用。我只有兩個簡單的函數:一個將圖像載入img節點,另一個插入i變量。第二個函數在定時器事件+計數後觸發,然後再次觸發image()函數。除非它不工作。簡單的Javascript XML幻燈片
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();
}
在正常情況下,兩者都可以工作。然而,我有一種感覺,nextImage作爲一個字符串意味着你正在尋找函數的頂層作用域,並且nextImage例程在被調用之前超出了作用域。如果您使用了符號名稱(並且可能在函數名稱上方定義了該函數),那麼您會很好。 – ijw 2011-05-09 21:20:29
區別在於你可以對第一個問題有一個範圍問題,因爲它可能無法訪問字符串中的函數。 – 2011-05-09 21:22:04