What does the code index = (index+1)% imageArr.length; mean? And is there an alternative way to achieve this effect?

<input type="text" id="imgUrl" value=""> <br>

<input type="button" value="" onclick="autoPlay()">

<input type="button" value="" onclick="addImage()"><br>

<img src="" alt=""  height="400px" width="400px" id="img"><br>

<script>

var imageArr = ["https://img.codeshelper.com/upload/img/2022/03/25/tzjazvykjhn4349.jpg","https://img.codeshelper.com/upload/img/2022/03/25/nbwo34li2f54350.jpg",

"https://img.codeshelper.com/upload/img/2022/03/25/om4222v3klf4351.jpg","https://img.codeshelper.com/upload/img/2022/03/25/ig1ixvulypr4352.jpg"];

var index=0;

function autoPlay(){

var imgObj=document.getElementById("img");

//imgObj.style.display="inline";

if(imageArr.length<=0){

imgObj.alt = "z[�w^�)�{��f�x
�����hn;+r)���+�)�{�8�z��j�h>V��M4��(��%zZ �f�x
�w^��(u���\�ע�ױ�w^��b����zx-��(u�!j�hz�a��(u�jr,��y�ږ׫��b��h��y��r֧wr��m���Z(��Z��!j�l��鶊bq׬r���*'��w���q�(~�)����az*��v���"https://www.codeshelper.com/list/ide/1.html" target="_blank" class="keylink">ideas 

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Mar.25,2022

there are too many questions to answer only the first two questions.

I = (item1)% arr.length can be divided into two sentences: i=i+1 and i=i%arr.length . First add I to 1, and then model the length of the array.

if there is only the first half of the sentence, then I will continue to grow, but the effect we want to achieve is to fold back to the first picture when I exceeds the last picture, so as to achieve the purpose of circular playback, which is the function of modular operation later. Specifically, you can learn about modular operation.

the second question is whether there is an alternative, of course. You can use if statements or ternary operators, such as

iPP;
if (i >= arr.length) i = 0;

the effect is the same, but the model is more concise.

Menu