Search code examples
jqueryclicktimeoutdelay

jQuery Delay a click function for each element in matched set


The following code is supposed to perform a click function on each element in the matching set delaying the click by 200ms before it takes effect on each element.

$('.panel').each(function(){
        window.setTimeout($('a.last').click(),pdel);
        pdel += 200;
    });

It all happens at the same time, the timeout function is not working.


Solution

  • I'm assuming pdel was declared outside of the each. But you'll want your setTimeout to use a function, otherwise you are passing the result of the .click() method to the setTimeout.

    Try this:

    var pdel = 0;
    $('.panel').each(function(){
        setTimeout(function(){
            $('a.last').click();
        }, pdel);
        pdel += 200;
    });