I reworked the example a bit more. Since you'll know the number of images you're sending from the server, you can pass that number to the client as num_images
. The client will dump the marquee content into the div
with marquee
class and then check to see if the images expected match the number found inside the marquee div
. If not, the loading is still happening and we keep checking every half second. Once loading has finished, we clear the setInterval
call and tell the marquee to begin.
As we discussed in chat, setTimeout
is effective when starting the marquee code because it executes the code in a separate event loop, giving the plugin a chance to "see" the newly loaded dynamic images. If the following isn't working for you, try increasing the setTimeout
delay slightly.
I've added a working example which doesn't use socket.io. In this pen, content is dynamically added to the marquee div
, two images at a time. The marquee won't initialize until it sees eight images inside of it. Similar to the code below, setInterval
is called to check if the expected number of images matches the actual number of images in the DOM.
HTML
<div class='marquee'></div>
Server JS
// Send the HTML along and the known number of images
// inside the string
socket.emit('your_event_name', {
marquee_content : 'html images and text go here',
num_images : 3
});
Client JS
var socket = io.connect('http://localhost')
images_expected = 0, // 'num_images' from socket.emit data
interval;
function startMarquee() {
$('.marquee').marquee({duration: 8000});
}
function checkImagesLoaded() {
if ($('.marquee img').length === images_expected) {
clearInterval(interval);
setTimeout(startMarquee); // fire in new event loop
}
}
function insertMarqueeContent(content) {
$(".marquee").html(content);
}
function initMarquee() {
interval = window.setInterval(checkImagesLoaded, 500);
}
socket.on('your_event_name', function (data) {
images_expected = data.num_images;
insertMarqueeContent(data.marquee_content);
initMarquee();
});