0

I have a tabs with 4 links.

I want click it via jQuery. I use this for first link:

$("a[data-search='a1']").delay(2000).trigger('click');

it works.

Now also I want do it for other link:

$("a[data-search='a1']").delay(2000).trigger('click');
$("a[data-search='a2']").delay(2000).trigger('click');
$("a[data-search='a3']").delay(2000).trigger('click');
$("a[data-search='a4']").delay(2000).trigger('click');

but it runs only once for first trigger.

also I tried interval method (please don't suggest it). I think I can not use trigger('click') multipile.

what's the wrong?

live demo: https://jsfiddle.net/1fvqvjb3/8/

4
  • What are you trying to do with the trigger? Commented Nov 18, 2015 at 21:19
  • please check demo. it's a full code. Commented Nov 18, 2015 at 21:24
  • That javascript does nothing at all in the fiddle. Commented Nov 18, 2015 at 21:49
  • because of fiddle. in local server works. Commented Nov 19, 2015 at 6:03

1 Answer 1

1

I could be mistaken on what it is you're trying to do, but it appears as if you want each link to be clicked in succession. The jQuery .delay() function is non-blocking, so it does not wait 2 seconds between each click; rather, there will be a single 2 second delay and then all 4 links will be clicked at the same time, which would certainly seem to appear as though only one link were being clicked.

I'm not sure what you mean by the "interval method" that you do not want suggested, but perhaps the best way to solve your problem is to use setTimeout().

(function f(i) {
    setTimeout(function() {
        $('a[data-search="a' + i + '"]').click();
        if (i < 4) f(i+1);
    }, 2000);
})(1);

This is an example of an IIFE, which I highly suggest reading up on if you're not already familiar with them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. but see, If I replace alert(i); with $('a[data-search="a' + i + '"]').click(); it runs 4 times normally with right i value. but with click() it only runs once! even if add alert,the alert runs once. I hope you underestand me. that is my problem.

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.