Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确定从数组中调用了哪些值以用于另一个函数?_Javascript_Arrays - Fatal编程技术网

Javascript 如何确定从数组中调用了哪些值以用于另一个函数?

Javascript 如何确定从数组中调用了哪些值以用于另一个函数?,javascript,arrays,Javascript,Arrays,我有下面的代码,称为onclick,希望能够在几秒钟后显示第二个图像,这取决于从数组中选择的图像-例如,如果显示1.png,那么我希望显示1s.png、2.png,然后显示2s.png等等 function displayImage () { var img_name = new Array("images/1.png", "images/2.png", "images/3.png"); var l = img_name.length;

我有下面的代码,称为onclick,希望能够在几秒钟后显示第二个图像,这取决于从数组中选择的图像-例如,如果显示1.png,那么我希望显示1s.png、2.png,然后显示2s.png等等

    function displayImage () {
            var img_name = new Array("images/1.png", "images/2.png", "images/3.png");
            var l = img_name.length;
            var rnd_no = Math.floor(l*Math.random());
            document.getElementById("imgHolder").src = img_name[rnd_no];
            }

我如何确定从数组中选择了哪个图像,然后在另一个函数中使用呢?

我认为了解JavaScript的工作原理和作用域的工作原理非常重要

您需要做的是公开这些信息,以便两个函数都可以访问。您已经定义了图像数组,并且已经确定了随机数,所以只需在函数外部声明它们,就可以使用它们,这样现在这些变量就可以使用了。在全局范围内定义变量通常是不好的做法,但这只是为了教学

var selectedImage;//在外部定义,以便可用于其他功能 函数显示图像{ var img_name=new Arrayimages/1.png、images/2.png、images/3.png; var rnd_no=Math.floorimg_name.length*Math.random; 选择图像=img_名称[rnd_编号]; document.getElementByIdimgHolder.src=selectedImage; } 函数另一个函数{ console.logselectedImage;
} 就我所理解的问题而言,您可以使用返回并使图像数组全局化

var img_name = new Array("images/1.png", "images/2.png", "images/3.png");
var img_class = '';
//this function will return the index of the image in array
function displayImage () {
   var l = img_name.length;
   var rnd_no = Math.floor(l*Math.random());
   document.getElementById("imgHolder").src = img_name[rnd_no];
   return rnd_no;
}

var nextimage;
function showImages(){
     //your logic to show the next image here
     nextimage = (displayImage ()+1)%img_name.length;
     var myVar = setInterval(function(){
     if(img_class==''){
        document.getElementById("imgHolder").src = img_name[nextimage];
        img_class = 's';
     }else{
        var img_arr = img_name[nextimage].split(".");
        if(img_arr.length===2){
           document.getElementById("imgHolder").src = img_arr[0]+"s."+img_arr[1];
        }

        img_class = '';
        nextimage = (nextimage+1)%img_name.length;
     }


   }, 1000);
}
var img_name=new Arrayimages/1.png、images/2.png、images/3.png; var img_class=; //此函数将返回数组中图像的索引 函数显示图像{ var l=img_name.length; var rnd_no=Math.floorl*Math.random; document.getElementByIdimgHolder.src=img_名称[rnd_编号]; 返回rnd_编号; } var下一代; 函数showImages{ //您在此处显示下一幅图像的逻辑 nextimage=displayImage+1%img_name.length; var myVar=setIntervalfunction{ ifimg_类=={ document.getElementByIdimgHolder.innerHTML=img_name[nextimage]; img_类='s'; }否则{ var img_arr=img_name[nextimage]。拆分。; ifimg_阵列长度===2{ document.getElementByIdimgHolder.innerHTML=img_arr[0]+s.+img_arr[1]; } img_类=; nextimage=nextimage+1%img_name.length; } }, 1000; } 展示图像;
可能是这样的

var images = ["images/1.png", "images/2.png", "images/3.png"];
function displayImage () {
    var dom = document.getElementById("imgHolder");
    if ( images.indexOf(dom.src) >=0 ) {
        dom.src = dom.src.replace(".png", "s.png"); // Seems like a stupid way to do this
        return;
    }
    var l = images.length;
    dom.src = images[Math.floor(l*Math.random())];
}

你需要一个全局变量来知道函数是否第一次运行

var alreadyRandom = false;

function displayImage()
{
  var img_name = new Array("images/1.png", "images/2.png", "images/3.png");
  if(alreadyRandom)
  {
    // If the random for first time is done
    var rnd_no = img_name.indexOf(document.getElementById("imgHolder").getAttribute('src'));
    if(rnd_no === img_name.length-1)
    {
      // If the current image is the last one in array,
      // go back to the first one
      rnd_no = 0;
    }
    else
    {
      // Choose the next image in array
      rnd_no++;
    }
    document.getElementById("imgHolder").src = img_name[rnd_no];
  }
  else
  {
    var l = img_name.length;
    var rnd_no = Math.floor(l*Math.random());
    document.getElementById("imgHolder").src = img_name[rnd_no];
    alreadyRandom = true; // Tell the function not to random again
  }
  return img_name[rnd_no];
}
演示:

此外,使数组全局化将使函数更高效


希望这能有所帮助,

正在尝试随机或有序地选择图像?我随机选择第一张图像,但如果这使得sensevar img=document.getElementByIdimgHolder;img.src=img.src.replace.png,s.png@安德烈亚斯-请问,在3秒钟后,用什么简单的方法将x.png更改为xs.png?我认为这可能是一种更快的方式来实现我的目标,在x秒后执行一个函数?->。但请注意,这可能会增加不必要的行为。每次调用都会启动一个新计时器。因此,如果有多次单击,将创建多个计时器,并对每个计时器进行处理并更改图像的src。因此,这需要一些调整:将它放在外部只会提高函数的效率,但这并不能解决问题。@Noobit问的是如何确定在另一个函数中使用它时调用了哪个值。顺便说一句,我已经编辑了要解决的答案,不确定它是否是最有效的答案。这并不能解决问题。你是否清楚地阅读了这个问题:它解决了他提出的问题——如何在不同的函数中引用变量?如果您指的是如何交换图像的问题,我希望OP澄清这一点,然后我可以相应地更改我的答案//这似乎是一种愚蠢的做法,那么为什么您添加我的注释作为答案而没有正确修复变量的名称?抱歉。当时我正在读别的东西,但我的评论仍然站着