This is really a simple affair, I just wonder if there is a better way to do it:
let el = document.querySelectorAll('#comic img');
let loadPromise = new Promise(function(resolve, reject) {
let ticker = setInterval(() => {
if (el[0].complete) {
clearInterval(ticker);
resolve();
}
}, 100);
setTimeout(() => {
clearInterval(ticker);
reject('Timeout');
}, 5000)
});
loadPromise.then(() => {
console.log(el[0].naturalWidth);
let container = document.querySelectorAll('.main-wrapper > div > section > section.column.column-1.grid-left-4.grid-width-16');
container[0].style.width = (el[0].naturalWidth + 420) + 'px';
});
I must wait for the image to load, so I know its size so that I can adjust an element on the page.
But setInterval
(aka polling the element) seems so... medieval. Is there a nicer way in modern JS?