I found code on SO that I was able to tweak to fit my needs but can't seem to get it all to work as desired. Basically I have some ul's whose li's fade in, then the list fades out and the process repeats itself through all ul's. I wan to add a delay between the time the ul's switch. So I'd like 'ul#one' to fade in it's li's then stay visible for 2 seconds, and then fade in the li's from 'ul#two'
Here is the markup:
<ul id="one" class="contracts">
<li>one</li>
<li>one</li>
<li>one</li>
</ul>
<ul id="two" class="contracts">
<li>one</li>
<li>one</li>
<li>one</li>
</ul>
...etc
and the js
function AnimateList($listItems, index, callback) {
if (index >= $listItems.length) {
$listItems.closest("ul.contracts").fadeOut(function() {
$listItems.css("left","400px").css("opacity",0); //reset
callback(); //next list
});
return;
}
$listItems.eq(index).animate({left:0, opacity:1}, 2500, function() {
AnimateList($listItems, index+1, callback)
});
}
function FadeLists($lists, index) {
if (index >= $lists.length) index = 0;
var $currentList = $lists.eq(index);
$currentList.fadeIn(function() {
AnimateList($currentList.find("li"), 0, function() { FadeLists($lists, index + 1)
});
})
}
var $allLists = $("ul.contracts")
FadeLists($allLists, 0);
I've tried adding a setTimeout around "FadeLists" but it doesn't seem to work.
function FadeLists($lists, index) {
if (index >= $lists.length) index = 0;
var $currentList = $lists.eq(index);
setTimeout($currentList.fadeIn(function() {
AnimateList($currentList.find("li"), 0, function() { FadeLists($lists, index + 1), 2500);
});
})
}
Neither did adding a time to the Fadelists argument.
setTimeout(FadeLists($allLists,0),2500);
How can I get it to keep each list visible for 2 seconds before running FadeLists again?
Per zero298 I tried the following, but it delays the initial run by the specified time and doesn't allow any delay between fading in the ul's
function AnimateList($listItems, index, callback) {
if (index >= $listItems.length) {
$listItems.closest("ul.contracts").fadeOut(function() {
$listItems.css("left","400px").css("opacity",0); //reset
callback(); //next list
});
return;
}
$listItems.eq(index).animate({left:0, opacity:1}, 1500, function() {
AnimateList($listItems, index+1, callback)
});
}
function FadeLists($lists, index) {
if (index >= $lists.length) index = 0;
var $currentList = $lists.eq(index);
setTimeout( () => $currentList.fadeIn(function() {
AnimateList($currentList.find("li"), 0, function() {
FadeLists($lists, index + 1) });
}));
}
var $allLists = $("ul.contracts")
// FadeLists($allLists, 0);
setTimeout(() => FadeLists($allLists,0),2500);