我正在尝试将函数内部填充的数组返回到另一个数组,因此我可以使用数组中的值进行幻灯片放映。
我的代码是:
var flag = 0
var slideimages = new Array()
slideimages = readfile()
function readfile(){
//read data into array x
flag =1
return x
}
if(flag == 1){
//execute slideshow functions
}
//slideshow functions definitions (for timeout n picture index etc)
//using slideimages[index] to choose pic
有人可以帮助我吗?
我的幻灯片显示没有运行,我猜是因为这些值没有被复制到slideimages []
编辑:
这是我的代码:
<html><head></head><body>
<div id="col1">
<img id="img_frame" class="lazy-img" name="slide1">
</div>
<div id="col2">
<img id="img_frame" class="lazy-img" name="slide2">
</div>
<div id="col3">
<img id="img_frame" class="lazy-img" name="slide3">
</div>
<div id="col4">
<img id="img_frame" class="lazy-img" name="slide4">
</div>
<div id="col5">
<img id="img_frame" class="lazy-img" name="slide5">
</div>
<script language="JavaScript1.1">
function getImages() {
var slideimages = new Array();
slideimages = readfile();
if(slideimages){
slideit1()
slideit2()
slideit3()
slideit4()
slideit5()
}
}
function readfile(){
var allText =[];
var Lines = [];
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "urls.txt", true);
allText = txtFile.responseText;
txtFile.onreadystatechange = function()
{
if (txtFile.readyState == 4 && txtFile.status == 200)
{// Makes sure it's found the file.
allText = txtFile.responseText;
Lines = allText.split(/\r\n|\n/);
}
else {
//alert("Didn't work");
}
}
txtFile.send(null)
return Lines
}
var slideshowspeed1=3000
var slideshowspeed2=27000
var slideshowspeed3=70000
var slideshowspeed4=11000
var slideshowspeed5=50000
var whichimage1=0
var whichimage2=100
var whichimage3=300
var whichimage4=500
var whichimage5=700
function slideit1(){
if (!document.images)
return
document.images.slide1.src=slideimages[whichimage1]
if (whichimage1<slideimages.length-1)
whichimage1++
else
whichimage1=0
setTimeout("slideit1()",slideshowspeed1)}//slideit1()
function slideit2(){
if (!document.images)
return
document.images.slide2.src=slideimages[whichimage2]
if (whichimage2<slideimages.length-1)
whichimage2++
else
whichimage2=0
setTimeout("slideit2()",slideshowspeed2)}//slideit2()
function slideit3(){
if (!document.images)
return
document.images.slide3.src=slideimages[whichimage3]
if (whichimage3<slideimages.length-1)
whichimage3++
else
whichimage3=0
setTimeout("slideit3()",slideshowspeed3)}//slideit3()
function slideit4(){
if (!document.images)
return
document.images.slide4.src=slideimages[whichimage4]
if (whichimage4<slideimages.length-1)
whichimage4++
else
whichimage4=0
setTimeout("slideit4()",slideshowspeed4)}//slideit4()
function slideit5(){
if (!document.images)
return
document.images.slide5.src=slideimages[whichimage5]
if (whichimage5<slideimages.length-1)
whichimage5++
else
whichimage5=0
setTimeout("slideit5()",slideshowspeed5)}//slideit5()
</script>
</body>
</html>
我真的不知道为什么我网页上的div没有显示图片(其网址在urls.txt文件中)。我真的很陌生。
答案 0 :(得分:0)
你真的不应该像这样嵌套函数。每次调用readfile时,它都会创建一个新的readfile函数对象,这是一个糟糕的设计,如果你不小心可能会使你的系统陷入困境。此外,你可以真正完成“旗帜”试图以更清洁的方式完成同样的事情。请尝试以下方法:
function getImages() {
var slideimages = readfile();
if (slideimages) {
// execute slideshow functions
},
function readfile() {
// read data into array x
return x;
}
您的问题可能与您如何实现“将数据读入数组x”有关。如果您仍然无法弄明白,请发布其余代码。