2

I have an array of jQuery objects. I'd like to interact with each item in the array if they are visible. My flow goes something like this:

  1. Create an array, whose elements are all jQuery objects.
  2. The objects may or may not be visible.
  3. Start with the first element in the array.
  4. Wait for the item to be visible, time is arbitrary. Say 3 seconds. If becomes visible, go to step 5, else return from 'play' function. // Updated
  5. Click the item.
  6. Repeat 4-5 until there are no more items left to be interacted with.

UPDATE (clarification of use case):
Essentially what I'm doing is performing a series of serial actions on an array. A-B-C-D, in order. However there may be an issue when A creates B's element and B has already been checked if it was visible. It won't get clicked on.

I've been looking at jQuery's deferred object, but am at a loss on how to implement it. How can this be implemented? Is there an alternative to using a deferred object?

As per @jmort's suggestion I'll attach what I have so far.

var play = function() {
    var dfds = []; 
    $(steps).each(function(i, v) {
        dfds[i] = $.Deferred();
    }); 

    $.when.apply(null, dfds).then(function(){
        console.log('all done');
    }); 

};  

Assume that steps equals the array of jQuery objects.

10
  • 1
    Perhaps include a code example of what you've tried so far or at least some of the code you're working with. It gives everyone something to work with when looking at your question. Commented May 10, 2012 at 7:31
  • @jmort253: Sure. I can do that. Thanks. Commented May 10, 2012 at 7:32
  • 1
    There's no need to use a deferred unless you want to implement some async scenario ("here are the items and here's what to do on them when X happens; I don't have a clue when X might happen or even what X might be"). Commented May 10, 2012 at 7:33
  • @Jon: So would a series of callbacks be better suited for this scenario? Essentially what I'm trying to do is this: execute A-B-C-D. But they have to be in order. Commented May 10, 2012 at 7:38
  • @kennethkoontz. Is my answer valid for that need? Or do you want to wait for all the steps elements to be visible? Commented May 10, 2012 at 7:40

1 Answer 1

2

It can be done easily:

var play = function() {
    $(steps).filter(':visible').click();
}; 

Update:

var play = function() {
    $(steps).each(function() {
        $(this).filter(':visible').click();
    });
};​
Sign up to request clarification or add additional context in comments.

7 Comments

Bah, this is a perfect answer for my question. Thanks. However, I forgot to add in the fact that it's possible for a step, when clicked, will create the next steps dom element. In which case, wouldn't this plow right through it? I'll update.
@kennethkoontz. What do you mean by " will create the next steps dom element"
Ok so the steps array is created, it may or may not include visible elements. Heres a scenario and lets just suppose the steps array includes [A,B,C,D] where A is visible, B is not, C, is, and D is. Also, lets assume that B will become visible when A is clicked. So when the array is processed, It's possible that A will be clicked and the next element will not be clicked (which you want to happen). Hope this makes sense.
@kennethkoontz. How much time after the click of A until B becomes Visible?
@kennethkoontz. The only thing that is important is: whether the click callback of A use async operations, like AJAX, setTimeout, callbacks.
|

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.