You need to create a recursion so that the function calls itself. A crutch was created:

function startSlider(items, delay) { $(items[0]).fadeIn(300) .delay(delay) .fadeOut(300) .promise() .done(function() { items.splice(0, 1); if (items.length > 0) { startSlider(items, delay); } else { //sliderRetry($(".offer-scrin"), delay); startSlider(items, delay); } }); } function sliderRetry(items, delay) { return startSlider(items, delay); } 

This all works, but on pages where this function is not needed, a "many recursion" error is caused.

How to do it right?

  • And how is it determined that "the function is not needed"? Can't check for the presence of an element for the slider? And if not, return false; . For example, the first line of the startSlider function startSlider to do if (!$(items[0]).length) return false; - cyadvert
  • Thank you, it works that way. But for the purity of the code, is it possible to somehow get rid of the second function? So that the function calls itself, and not through another? - Anton
  • So in general, it is not clear why you need the second function. sliderRetry is not mentioned anywhere in the code. But startSlider launches itself so. - cyadvert
  • @cyadvert Please post your comments as a response. - Nicolas Chabanovsky

1 answer 1

The most reasonable option, I think, is to put a check on the presence of an element for the slider.
If the item is not present, the function will return false , and the error will cease to appear. This is best done at the very beginning.

 function startSlider(items, delay) { if (!$(items[0]).length) return false; //если элемента нет - выход $(items[0]).fadeIn(300) .delay(delay) .fadeOut(300) и т.д. 

And yet, it is not entirely clear why the sliderRetry function is sliderRetry ? It is not used anywhere in the code you submit ...