I'm still grappling with async await and often end up using a set timeout to quickly solve my problem hackily.
I'm getting data with jquery get and calling the buildSlideshow function on .done();
After the slideshow is built, I want to call the startSlideshow()
function with another function.
I've tried various versions of async and await but can't seem to nail it, and without a timeout of some sort, the slideshow starts before it's fully build, with funky results.
$.get('/data/backgrounds.json').done((data) => {
bgData = data;
buildSlideshow(bgData.find(findVenue));
});
function findVenue(page) {
return page.page === currentPage;
}
function buildSlideshow(page) {
page.bgs.forEach((bg) => {
bgUrl = bg.url ? bg.url : '';
venueName = bg.venue ? bg.venue : '';
venueLocation = bg.cityState ? bg.cityState : '';
caption = `${venueName}<br>${venueLocation}`;
div = document.createElement('div');
$(div).append(caption);
$(div).css('background-image', `linear-gradient(180deg, rgba(8, 0, 14, 0.3) 0%, rgba(8, 0, 14, 0) 100%), url(${bgUrl})`);
$('#bgSlideshow').append(div);
});
}
function startSlideshow() {
$('#bgSlideshow > div:gt(0)').hide();
setInterval(() => {
$('#bgSlideshow > div:first')
.fadeOut(2000)
.next()
.fadeIn(2000)
.end()
.appendTo('#bgSlideshow');
}, 8000);
}
// doesn't work
startSlideshow();
// does work
setTimeout(() => {
startSlideshow();
}, 300);