简体   繁体   English

循环在数组元素上停滞

[英]Loop stalls on array element during iteration

I'm trying to cycle iterate through an array by using a 'previous' and 'next' button, respectively moving through the array. 我试图通过分别使用“上一个”和“下一个”按钮在数组中移动来循环遍历数组。

It works fine until you change 'direction' and try say, for example 'next' then 'previous' 直到您更改“方向”并尝试说,例如“下一个”然后“上一个”,它都可以正常工作

The array displays the same element it was already on instead of showing the previous element of the array. 数组显示的元素已经存在,而不显示数组的前一个元素。

For example, I am on 4, hit the 'previous' button and it remains on 4. You have to press the 'previous' button again to make it go back down to 3. 例如,我在4上,单击“上一个”按钮,它仍然在4上。您必须再次按下“上一个”按钮,以使其回到3。

What am I missing here...? 我在这里想念什么...?

 var numbers = ["I am index number 0", "I am index number 1", "I am index number 2", "I am index number 3", "I am index number 4", "I am index number 5"]; var i=0; var text = ""; document.getElementById("back").addEventListener("click", function previous() { if (i-1 >= 0) { var text = numbers[--i]; } document.getElementById("filler").innerHTML = text; }); document.getElementById("forward").addEventListener("click", function next(){ if (i < numbers.length) { var text = numbers[i++]; } document.getElementById("filler").innerHTML = text; }); 
 <p id="filler"></p> <button id="back">PREVIOUS</button> <button id="forward">NEXT</button> 

Actually what your looking is pre-increment and not post-increment. 实际上,您所看到的是递增之前的值,而不是递增之后的值。 ++i increments the value then returns the value, in the other hand i++ returns the current value and then increments the value. ++i递增值,然后返回值,另一方面, i++返回当前值,然后递增值。

Also another one advice, you don't need to create all the time the var text , once you create and define it empty, you better only do text = numbers[--i] without var . 还有另一条建议,您不需要一直创建var text ,一旦创建并定义为空,最好只使用text = numbers[--i]而不使用var

Another thing I noticed is that in your second conditional (if) you should do if (i < numbers.length - 1) because .length method starts counting in 1. 我注意到的另一件事是,在第二个条件(if)中,应该执行if (i < numbers.length - 1)因为.length方法从1开始计数。

I made a jsfidddle for you :) https://jsfiddle.net/t2cjf5m8/16/ 我为你做了一个jsfidddle :) https://jsfiddle.net/t2cjf5m8/16/

First of all you should look at the comment made by Crayon Violent 首先,您应该看看Crayon Violent的评论

--i decrements the value then returns the value. --i递减该值,然后返回该值。 i++ returns the current value then increments. i ++返回当前值,然后递增。 If you want them to behave the same, use --i and ++i or do the operation outside of the index reference. 如果希望它们的行为相同,请使用--i和++ i或在索引引用之外进行操作。

You should not redefine the text inside the ifs 您不应该在ifs中重新定义文本

var text = numbers[--i];

This solution below works: 下面的解决方案有效:

  var numbers = ["I am index number 0", "I am index number 1", "I am index number 2", "I am index number 3", "I am index number 4", "I am index number 5"]; var i = -1; var text = ""; document.getElementById("back").addEventListener("click", function previous() { if (i > 0) { text = numbers[--i]; } document.getElementById("filler").innerHTML = text; }); document.getElementById("forward").addEventListener("click", function next(){ if (i < numbers.length - 1) { text = numbers[++i]; } document.getElementById("filler").innerHTML = text; }); 
 <p id="filler"></p> <button id="back">PREVIOUS</button> <button id="forward">NEXT</button> 

I modified your code below to make it work: 我在下面修改了您的代码以使其正常工作:

<p id="filler"></p>
<button id="back">PREVIOUS</button>
<button id="forward">NEXT</button>


<script>
    var numbers = [
        "I am index number 0",
        "I am index number 1",
        "I am index number 2 ",
        "I am index number 3",
        "I am index number 4",
        "I am index number 5"
    ];
    var i = 0;
    var text = "";



    document.getElementById("back").addEventListener("click", function previous() {
        if (i - 1 >= 0) {
            var text = numbers[i--];
            document.getElementById("filler").innerHTML = text;
        }
    });




    document.getElementById("forward").addEventListener("click", function next() {
        if (i < numbers.length) {
            var text = numbers[i++];
            document.getElementById("filler").innerHTML = text;
        }
    });
</script>

Only change I made is in var text = numbers[i--]; 我所做的唯一更改是在var text = numbers[i--]; I changed the position of the operator from left to right. 我从左到右更改了操作员的位置。 "If the operator appears before the variable, the value is modified before the expression is evaluated. If the operator appears after the variable, the value is modified after the expression is evaluated." quoting from: https://docs.microsoft.com/en-us/scripting/javascript/reference/increment-and-decrement-operators-javascript 引用自: https : //docs.microsoft.com/en-us/scripting/javascript/reference/increment-and-decrement-operators-javascript

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM