0

I have some script that is calling AJAX information from a server and then displaying the information into blocks on the page. And every 8 seconds those blocks will fade to a new set of information.

The information from the server is stored in a fixed queue that pushes new items to it every 8 seconds.

And for each block I have it grab a random item from that array to show. The only thing is my blocks are getting a lot of duplicates.

Is there a way to check and see if that array item has been called from another block, and if so it will move on to find another item not in use.

    var queue = FixedQueue( 50 );

    $(document).ready(function() {
     $('.submit').click(function(){
      setInterval(function(){
       queue.push(fulltweet);
     },8000);
    });
   });

    setInterval(function(){
        randotime = intervals[Math.floor(Math.random()*intervals.length)];
            $('.block1', '.photo1:nth-child(1)').queue(function(){
                $(this).hide();
                $(this).html(queue[0]);
                $(this).fadeIn(2000);
                $(this).delay(randotime);
                $(this).dequeue();
            });
            $('.block1', '.photo1:nth-child(1)').fadeOut(2000);
    },randotime);

I was creating a random number based on the length of the queue and using that to call queue[rando] but again, I keep getting duplicates in the blocks.

2
  • 1
    Can you post your code?
    – Blender
    Commented Jun 20, 2013 at 21:49
  • Why do you choose a random one and not just the first? When you read the first value from a queue, the value is usually removed as well and the second value becomes the first. You would never choose the same value twice then. Commented Jun 20, 2013 at 21:49

1 Answer 1

0

First, if you don't want duplicate items in your array, don't let them to be duplicate. Before inserting new items to your array, control whether it exists in your array or not. Using equality operation may not work if you use objects instead of primitive types(string, integer, etc..). Therefore you need a function checks if a given element exists in the array before insertion, and this function must use a equals function which compares two given objects.

Secondly, javascript allows you to add fields to objects in runtime. So when a block reaches and displays an object, you can put a field over this object. Let's say its name is 'inuse'.

When block A reaches the object:

object.inuse = true;

When block B reaches the same object randomly:

var object = pickRandomly();
while (object.inuse) {
    object = pickRandomly();
}
// here's the unique object which is not used by another block.

I hope that helps. If you can provide a sample code, I can provide a better answer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.