简体   繁体   English

如何使阵列每次运行都能选择不同的选择?

[英]How can I make my array choose different choices each time it runs?

Right now, I have an array with three options. 现在,我有一个包含三个选项的数组。 I have a function set up so it chooses one option each time, but sometimes it repeats outputs. 我设置了一个函数,因此每次都选择一个选项,但有时会重复输出。 How can I code it so the same color does no repeat twice in a row? 我该如何编码,以便同一颜色不会连续重复两次?

<!DOCTYPEhtml>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Tester</title>
    <script type="text/javascript">

        var colors = ['#ce0e0e', '#079b0c', '#3e3fd6'];

        function changeColor(){
            var randomColor = colors[Math.floor(Math.random() * colors.length)];
            while (myDiv.style.backgroundColor == randomColor)
                randomColor = colors[Math.floor(Math.random() * colors.length)];

            myDiv.style.backgroundColor = randomColor;
        }
    </script>
    <style>
        .divClass {
            width:300px;
            height:300px;
            border: 1px solid black;
            border-radius:1000px;
            background-color:transparent;
        }
    </style>
</head>
<body>

    <div class=divClass id="myDiv" style="border:6px solid black;">
    </div>

    <input id=button type=button value="chagne color" onclick='changeColor()'>

</body>
</html>

I tried to use the unique = true but that didn't seem to work. 我尝试使用unique = true,但这似乎没有用。

<script type="text/javascript">

        var colors = ['#ce0e0e', '#079b0c', '#3e3fd6'];
        var old;

        function changeColor(){

            var randomColor = colors[Math.floor(Math.random() * colors.length)];
            if(old){

                if(old !== randomColor){


                    myDiv.style.backgroundColor = randomColor;
                    old = randomColor;
                } else {
                    setTimeout(changeColor,300);
                }
            } else {
                myDiv.style.backgroundColor = randomColor;
                old = randomColor;



            }
        }
    </script>

They won't repeat now. 他们现在不再重复。 Old keeps the value of the first/previous click. “旧”保留第一次/上一次点击的价值。 If you get same value you relaunch function until it changes. 如果获得相同的值,则重新启动功能,直到功能更改为止。

50+ clicks and never same color twice 50次以上的点击,绝不会两次出现相同的颜色

var color = colors[Math.floor(Math.random() * colors.length)];    
function changeColor(){ 
        var newColor = colors[Math.floor(Math.random() * colors.length)];
        if(newColor==color){
          changeColor();
        } else {
          color = newColor;
          myDiv.style.backgroundColor = color;
        }
    }

you should use a recursive function 您应该使用递归函数

暂无
暂无

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

相关问题 我怎样才能在 axios 的反应中让我的每个测验问题及其在不同页面中的选择 - How can I have each of my my quiz questions and their choices in different pages in react by axios 每次从下拉列表中选择项目时,如何使图表动态化? - How can I make my charts dynamic every time I choose an item from my dropdown? 如何根据用户的选择制作不同的文本? - How can I make a different text based on the user's choices? 每次执行 function 时,如何将数组另存为新的 object 而不会覆盖我的旧数组 - how can you make an array save as a new object each time a function is executed and not overwrite my old array 如何使数组选择四个不同的选项 - How to make array choose four different options 每当数组长度增加时,如何动态地向页面添加新内容? - How can I dynamically add new content to my page each time my array length increases? 如何使我的索引数据数组成为多个取决于它们的 TIME IN 和 Event Time Started - How can i make my index data array to be multiple depends on their TIME IN and Event Time Started 如何确保每次从下拉列表中选择图表时都会加载图表? - How can I ensure that my charts diagram load every time I choose it from my dropdown? 每次运行时如何从Google表格中提取不同的值 - How to pull a different value from google sheets each time it runs 如何在我的函数参数中使对象数组调用每个对象的函数? - How can I make an array of object in my function parameter call a function for each object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM