2

I'm using the below js plugin to do my text scrolling: https://github.com/aamirafridi/jQuery.Marquee

Basically we just call the below for text scrolling:

$('.marquee').marquee();

But I plan to put image into the scrolling text too. Like what it mentioned in the readme file, we need to put the $(window).load like below so that it can calculate the image width accurately:

$(window).load(function() { $('.marquee').marquee(); });

This function will load the marquee after the webpage load out.

But what if I only load the marquee after I received a command. For example:

receivedCom = function(){ $('.marquee').marquee({duration: 8000}); });

I have try putting $(window).load into the receivedCom function. It won't run. Any idea how?

Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31
Coolguy
  • 2,225
  • 12
  • 54
  • 81

2 Answers2

2

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();
});
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
  • Let me explain, the author of the js mentioned that if I put image into his plugin, I will have width inaccuracy issues. He mentioned that it can be solve by using the $(window).load. But if I put this $(window).load into a function, it can't work. – Coolguy Apr 18 '16 at 06:34
  • @Coolguy I modified the above code a little so that it more closely matches what you're asking. – Andy Hoffman Apr 18 '16 at 07:03
  • I'm not using a button to trigger. Actually I'm using socket.io. When I received a command from the server using socket.io, I trigger the scrolling image to run. – Coolguy Apr 18 '16 at 07:12
  • I've modified the above code to (hopefully) work with socket.io. – Andy Hoffman Apr 18 '16 at 07:15
  • I'm not only loading one image. I have multiple image. How do I change the #myImage id? – Coolguy Apr 18 '16 at 07:19
  • Edited the code to call the marquee code after all images have been loaded. – Andy Hoffman Apr 18 '16 at 07:27
  • But you still only have one image. How do we do if we have 5 image? Can apply on this $('img').on('load', startMarquee); too? – Coolguy Apr 18 '16 at 07:31
  • I have to mentioned that it is from the server command that tell me what image to put into the scrolling image. – Coolguy Apr 18 '16 at 07:34
  • @Coolguy Yes, this will call the startMarquee function after all images have loaded. I just happened to use one image in the example code, but any number of images should work. – Andy Hoffman Apr 18 '16 at 07:34
  • I have to mentioned that it is from the server command that tell me what image to put into the scrolling image. So the 'your_event_name' is actually the server sending me the image name and location. After getting this info, I will put it into the scrolling image. – Coolguy Apr 18 '16 at 07:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109403/discussion-between-andrew-hoffman-and-coolguy). – Andy Hoffman Apr 18 '16 at 07:37
0

If i understand your question correctly, 'after I received a command' means you are looking for an event.

There are lots of techniques depending on the type of event you want to listen to.

For example, you might want to listen for a mouseclick :

$(document).ready(function(){
  $(window).click(function(event, target){
    $('.marquee').marquee();
  });
});

JSfiddle here : http://jsfiddle.net/knr2qzxu/ If you want to play around with it a bit :)

I strongly suggest you read more about the jQuery events here if you want to have a deeper understanding on what's going on here : https://api.jquery.com/category/events/

EDIT :

Ok I went through your question again, correct me if I don't get it correctly :

You somewhere in your js code have a function which is written as such : receivedCom = function(com) { }; If so just check if the window has been loaded before you call the marquee function :

var receivedCom = function(com) {
  if (document.readyState === "complete") {        
    $('.marquee').marquee(); 
  }
};

And there you go : The js checks if your window is displayed correctly, then triggers the marquee.

You can also use a setInterval to check every n miliseconds if the window has loaded, and then trigger the marquee, please read the doc if you need to do this : http://www.w3schools.com/jsref/met_win_setinterval.asp

Please tell me if it worked !

  • It's not a mouse click event. It's receiving a command event... the command can be from tcp/ip or comport. Any idea how to do it? – Coolguy Apr 18 '16 at 06:36
  • Ok I went through your question again, correct me if I don't get it correctly : – o0Ignition0o Apr 18 '16 at 06:39
  • Actually I'm using socket.io, when I received a command from a server, I will load the scrolling image – Coolguy Apr 18 '16 at 06:44
  • If you use socket io, there must be some event when you receive the message. You theen need to check for the document.readyState (see the edited snippet). If you only get 1 event, you may want to use a setInterval to check periodically for the readyState before you trigger the marquee :) If so, don't forget to clear it ! – o0Ignition0o Apr 18 '16 at 06:54
  • I've tried your way, but I still have the image width inaccuracy issue. Any idea how to solve it? – Coolguy Apr 18 '16 at 07:01
  • Well without access to the code, I have to admit I'm running out of options. The answer below (Andrew Hoffman) looks pretty good by the way, perhaps it will do the trick – o0Ignition0o Apr 18 '16 at 07:08
  • Image width inaccuracy issue means that all the image not yet finish scrolling, it started all over again. By right, it should scroll finish all the image first before starting again. Seems like it can't get the width right. – Coolguy Apr 18 '16 at 07:11
  • As shown in one of the marquee examples : http://codepen.io/aamirafridi/pen/ayBmF it's considered good practice to destroy / create the marquee everytime there s a data change, have you tried to : - Create it on $(window).load() - Destroy it a recreate it on command event ? – o0Ignition0o Apr 18 '16 at 07:18
  • In fact, my image scrolling will not come out during window load. It will only come out when I received the command from server. I mentioned window load because the author of this plugin says that window load can solve the image width inaccuracy problem. But can't use on me because I only will show the scrolling image when I receive command. So, how to solve the problem? – Coolguy Apr 18 '16 at 07:29
  • Can you please copy paste your html / js and css on a jsfiddle (with the image and all) ? I can't help you any further without seeing the full code :/ – o0Ignition0o Apr 18 '16 at 07:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109405/discussion-between-coolguy-and-o0ignition0o). – Coolguy Apr 18 '16 at 08:16