简体   繁体   English

如何确定已从数组调用了哪个值以在另一个函数中使用?

[英]How do I determine which value has been called from an array, to use in another function?

I have the below code which is called onclick, and want to be able to show a second image after a few seconds, depending on which one is chosen from the array - so for example if 1.png is shown, then I want to show 1s.png, 2.png then show 2s.png etc 我有下面的代码,称为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];
            }

How do I determine which image has been chosen from the array to then use in another function please ? 如何确定从阵列中选择了哪个图像,然后将其用于其他功能?

You're gonna need a global var for knowing that function is running for the first time or not. 您将需要一个全局变量,以了解该函数是否第一次运行。

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];
}

Demo: http://jsbin.com/kanejugahi/edit?html,js,console,output 演示: http : //jsbin.com/kanejugahi/edit?html,js,console,output

Moreover, making the array global would make your function more efficient. 此外,将数组设置为全局数组将使您的功能更高效。

Hope this helps, 希望这可以帮助,

I think it's important you understand how Functions work in JavaScript and how scope works. 我认为了解JavaScript中的功能如何工作以及作用域如何工作很重要。

What you need to do is you need to expose that information so that both functions have access. 您需要做的是公开这些信息,以便两个功能都可以访问。 You have the array of images defined and you have already determined the random number so just make them available by declaring them outside of the function so now these variables are available. 您已经定义了图像数组,并且已经确定了随机数,因此只需在函数外部声明它们即可使它们可用,从而使这些变量现在可用。 Defining variables in the global scope is generally bad practice but this is simply for teaching. 在全局范围内定义变量通常是不好的做法,但这只是为了教学。

 var selectedImage; // defined outside so it is available for other functions function displayImage() { var img_name = new Array("images/1.png", "images/2.png", "images/3.png"); var rnd_no = Math.floor(img_name.length * Math.random()); selectedImage = img_name[rnd_no]; document.getElementById("imgHolder").src = selectedImage; } function anotherFunction() { console.log(selectedImage); } 

As far as I understand the question, you can use a return and make the image array global. 据我了解的问题,您可以使用return并将图像数组设为全局。

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 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").innerHTML = img_name[nextimage]; img_class = 's'; }else{ var img_arr = img_name[nextimage].split("."); if(img_arr.length===2){ document.getElementById("imgHolder").innerHTML = img_arr[0]+"s."+img_arr[1]; } img_class = ''; nextimage = (nextimage+1)%img_name.length; } }, 1000); } showImages(); 
 <div id="imgHolder"></div> 

Mayve something like this? 可能会这样吗?

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())];
}

暂无
暂无

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

相关问题 如何在JavaScript函数中使用由另一个api设置的变量? - How can i use the variable which has been set up by another api, in my javascript function? 调用Inter函数后,如何使其无法执行 - How do i disable the inter function from being executed after once it has been called 如何在调用事件后延迟执行功能? - How do I delay a function from executing after an event has been called? 如何在不设置全局变量的情况下确定函数是否已被调用 - How to determine if a function has been called without setting global variable 如何确定是否在 class 方法中使用 jest 调用来自另一个库的常量 function - How do I determine if a constant function from another library is called in a class method using jest 萤火虫-如何检测已调用的函数或从何处调用了函数 - firebug - how can I detect what functions have been called or from where a function has been called 如何使用for语句和数组确定哪个元素被点击 - How to determine which element has been clicked using a for statement and array 如何在 javascript function 中使用已传递给哈巴狗模板的数组而不将其拆分? - How do I use an array that has been passed into a pug template, in a javascript function without it being split? 在另一个函数内部已调用了如何测试一个函数 - How to test a function has been called inside another function 如何确定是否已调用突变? - How to determine if mutation has been called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM