I have a script that does 2 main things in a sequence:
- It pulls data from an XML file through a GET request, and it appends the result into the DOM.
- I have this "MacyJS" script which basically puts the content within a nicely calculated grid (a bit like Masonry).
My issue is that the first part is taking quite some time (I don't mind, it's meant for my own private use) to get the result and append it to the DOM. So that the "MacyJS" already ran before, and thus not rendering properly the HTML in a nice grid.
You can see I tried to put a delay for MacyJS, but of course this is not precise and doesn't always work in time, since the GET/append sometimes takes more than 1.5s.
How can I automatically run "MacyJS" only once the DOM has fully been updated with the new HTML/content ?
Thanks a lot for your help.
Code below for reference:
$(document).ready(function(){
// THE FIRST SCRIPT
var x=20;
$.get("my xml file here", function(data) {
var $xml = $(data);
$xml.find("item").each(function(i, val) {
var $this = $(this),
item = {
description: $this.find("description").text(),
guid: $this.find("guid").text()
};
$('#grid').append($('<div class="parsed appear"><a target="_blank" href="' +item.guid +'">' +item.description +'</a></div>'));
return i<(x-1);
});
});
// THE SECOND SCRIPT
setTimeout(function(){
var macy = Macy({
container: '#grid',
margin: 20,
columns: 4,
});
}, 1500);
});