I would like to execute a scroll to
function after all the elements for the section that I would like to scroll to have been created. I thought promises
would be the way to go. This is what I have tried but it is not working, not sure how to write promises the right way when they are not used for ajax request:
This is the script:
var showMagazineDetail = function showMagazineDetail(id, slug, name, summary, issueImage, magazineImage, page){
images = [];
nextImage = 0;
imagesIndex = 0;
loadedImages = [];
scrollPoint = document.height;
window.location.hash = slug;
let createMagazineDetails = new Promise((resolve, reject) => {
$(".magazine-section").css('visibility', 'visible');
$('#name').text(name);
$('#summary').text(summary);
$('#cover-image').attr({"src" : '/imagecache/cover/' + issueImage, "alt" : name});
if (magazineImage != '') {
$('#issueImage').show();
$('#issueImage').attr({"src" : '/imagecache/medium/' + magazineImage, "alt" : name});
}else {
$('#issueImage').hide();
$('#issueImage').attr({"src" : '', "alt" : name});
}
// $('body').css('overflow-y', 'scroll');
$.getJSON("issue/images",
{ id: id },
function(result){
if (result.length < 2){
$('.magazine-preview-nav').hide();
} else {
$('.magazine-preview-nav').show();
}
$.each(result, function(i, value){
images.push(value);
});
preload();
});
});
createMagazineDetails.then(() => {
$('html,body').animate({
scrollTop: $(".magazine-detail").offset().top - 80
});
});
};
preload = function() {
for (i = 0; i < 4; i++) {
if (nextImage < images.length) {
var img = new Image();
img.src = '/imagecache/cover/' + images[nextImage];
loadedImages[nextImage] = img;
++nextImage;
}
}
}