简体   繁体   English

如何将函数返回的数组复制到 JavaScript 中的另一个数组中?

[英]How do I copy an array returned by a function into another array in JavaScript?

I'm trying to return an array populated inside a function to another array, so I can use the values inside the array for a slideshow.我正在尝试将一个函数内填充的数组返回到另一个数组,因此我可以将数组内的值用于幻灯片放映。

My code is :我的代码是:

       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

Could anyone help me out here?有人可以帮我吗?

My slideshow isn't running, and I'm guessing it's because the values aren't getting copied into the slideimages[]我的幻灯片没有运行,我猜这是因为这些值没有被复制到幻灯片中[]

edit:编辑:

Here is my code:这是我的代码:

<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>

I don't really know why the divs on my webpage aren't displaying the images (whose urls are in the urls.txt file).我真的不知道为什么我网页上的 div 不显示图像(其网址在 urls.txt 文件中)。 I'm really new to this.我真的很陌生。

You really shouldn't be nesting functions like that.你真的不应该像那样嵌套函数。 Every time you call readfile it will create a new readfile function object, which is bad design and can bog down your system if you're not careful.每次调用 readfile 时,它​​都会创建一个新的 readfile 函数对象,这是糟糕的设计,如果您不小心,可能会使您的系统陷入困境。 Also, you can really accomplish the same thing that "flag" is trying to accomplish in a much cleaner way.此外,您确实可以以更简洁的方式完成“flag”试图完成的相同事情。 Try the following:请尝试以下操作:

function getImages() {
  var slideimages = readfile();
  if (slideimages) {
    // execute slideshow functions
},

function readfile() {
  // read data into array x
  return x;
}

Your issue is probably with how you implemented "read data into array x."您的问题可能与您如何实现“将数据读入数组 x”有关。 If you still cant figure it out, please post the rest of the code.如果您仍然无法弄清楚,请发布其余代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何访问javascript函数返回的数组中的对象 - How do i access the objects inside the array returned by a javascript function Javascript:如何将单个元素的值从数组复制到同一位置的另一个数组中? - Javascript: how do I copy the value of a single element from an array into another array at the same position? 如何复制另一个数组中的数组(Javascript) - How to copy an array in another array (Javascript) 如何在Javascript / JQUery中将返回的JSON对象整形为数组? - How do I shape the returned JSON object into array in Javascript/JQUery? 如何根据Vue JavaScript中返回的键分割此数组? - How do I segment this array according to returned key in Vue JavaScript? 如何将返回的JavaScript数组项添加到特定文本框 - How do I add returned JavaScript array items to specific textboxes 如何访问函数返回的数组中的数据? - How do I access data in an array that was returned by a function? Javascript数组如何为数组中的每一行做一个功能 - Javascript array how do i do a function for each line in array 如何基于JavaScript中的另一个数组比较和更新此数组? - How do I compare and update this array based on another array in JavaScript? 在Javascript中,如何将多维数组除以另一个数组 - In Javascript how do I divide a multidimensional array by another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM