Waiting for an element present for N seconds before interacting with the element with javascript and jQuery lib

I have the following code:

var play = function() {
    $(steps).each(function(i, e) {
        $(this).is(':visible') 
            ? $(this).click()
            : console.log($(this).html() + ': not visible');
    });
};

The problem with this code is that it immediately clicks on an element if it is visible. However, I would like to wait / poll for an item that will be present for up to N seconds before clicking on the item. Any suggestions on how I can implement this? Thank!

+3
source share
4 answers

Then use setTimeout(). It is up to you how you go / getN

var play = function() {
    $(steps).each(function(i, e) {

        //preserve target since "this" in the timeout may be different
        var target = $(this);

        setTimeout(function(){
            if(target.is(':visible')){
                 target.click();
            }
        },N); //Click and check after N milliseconds

    });
};
0
source

What about...

setTimeout(function(){ $(this).click(); }, 5000) // 5 sec
0
source
var play = function() {
    $(steps).each(function(i, e) {
        $(this).is(':visible') 
            ? setTimeout(function(){ $(this).click(); }, TIME),
            : console.log($(this).html() + ': not visible');
    });
};

TIME - , :)

0

, setInterval . , - :

http://jsfiddle.net/GVu98/

Basically, just adjust the interval, which will continue until you see what you expect, and clear the interval.

0
source

All Articles