简体   繁体   English

在单页 Jquery/Vanilla Javascript 上超出了多个轮播最大调用堆栈

[英]multiple carousel maximum callstack exceeded on single page Jquery/Vanilla Javascript

So I'm trying to implement an infinite autoplay multiple carousel using Jquery.所以我正在尝试使用 Jquery 实现无限自动播放多个轮播。 I have created two different carousel blocks on same page this is the body of the page我在同一页面上创建了两个不同的轮播块这是页面的主体

<div class="rotating_block">
    <div class="block one"></div>
    <div class="block two"></div>
    <div class="block three"></div>
</div>
<br>
<div class="rotating_block">
    <div class="block one"></div>
    <div class="block two"></div>
    <div class="block three"></div>
</div>

CSS for the same CSS 相同

.rotating_block {
  display: flex;
}
.one {
  background: red;
  height: 100px;
  width: 100px;
}

.two {
  background: green;
  height: 100px;
  width: 100px;
}

.three {
  background: blue;
  height: 100px;
  width: 100px;
}

In JS i've created slideIndex array and childrens (blocks) array for each carousel slideIndex[n] stores current slide number to show while childrens[n] stores array of blocks/childrens在 JS 中,我为每个轮播创建了 slideIndex 数组和子(块)数组 slideIndex[n] 存储要显示的当前幻灯片编号,而 childrens[n] 存储块/子数组

For each parent div '.rotating_block' I'm storing its slideIndex and its children in those arrays and performing the carousel function.对于每个父 div '.rotating_block',我将其 slideIndex 及其子项存储在 arrays 中并执行轮播 function。 At the end I'm calling for setTimeout so as to run the function once again every five seconds and change the slide to give carousel like effect the problem is I'm getting max call exceeded stack / console getting logged every second multiple times最后,我调用 setTimeout 以便每五秒再次运行 function 并更改幻灯片以提供类似轮播的效果问题是我得到最大调用超出堆栈/控制台每秒多次记录

var slideIndex = [];
  var childrens = [];
  $(".rotating_block").each(function (index) {
    slideIndex[index] = 0;
    childrens[index] = $(this).find(".block");
    function carousel(children, slideIndex) {
      console.log('hello world');
      for (let i = 0; i < children.length; i++) {
        $(children[i]).hide();
      }
      if (slideIndex > children.length) {
        slideIndex = 1;
      }
      $(children[slideIndex - 1]).show();
      setTimeout(carousel(children, slideIndex), 5000);
    }
    carousel(childrens[index], slideIndex[index]);
  });

link to pen链接到笔

The problem is that you are passing the function carousel with parameters which is actually calling the function.问题是您正在传递带有参数的 function carousel ,实际上调用的是 function。

setTimeout(carousel(children, slideIndex), 5000);

To solve this, pass an arrow function to setTimeout要解决此问题,请将箭头 function 传递给 setTimeout

setTimeout(() => carousel(children, slideIndex), 5000);

EDIT编辑

Also, you forgot to increment the slideIndex value:此外,您忘记增加 slideIndex 值:

var slideIndex = [];
var childrens = [];
$(".rotating_block").each(function (index) {
  slideIndex[index] = 0;
  childrens[index] = $(this).find(".block");
  function carousel(children, slideIndex) {
    console.log(slideIndex);
    for (let i = 0; i < children.length; i++) {
      $(children[i]).hide();
    }
    //////////////////////
    slideIndex++;
    //////////////////////
    if (slideIndex > children.length) {
      slideIndex = 1;
    }
    $(children[slideIndex - 1]).show();
    setTimeout(() => carousel(children, slideIndex), 5000);
  }
  carousel(childrens[index], slideIndex[index]);
});

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

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