开发者

Jquery wait and click more times

开发者 https://www.devze.com 2023-03-04 17:05 出处:网络
I have a question because i don\'t understand this. I want to wait between showing the next tab with jquery and coded something like this:

I have a question because i don't understand this.

I want to wait between showing the next tab with jquery and coded something like this:

$('#tabs ul .ui-corner-top').each(function(){
    setTimeout(function(){},2000)
    $(this).children('a').click();
});

I've expected to see waiting for 2 sec if the next tab will be clicked. But the Timeout is only starting once and all hrefs are immediate clicked without waiting.

Then I tried something different:

for (i=0;i<$('#tabs ul .ui-corner-top').size();i++) {
    setTimeout('changeTab(i)',i*3000);
}

function changeTab(i) {
    clearTimeout(timeout);
    timeout=setTimeout(function(){
        $('#mainContent').masonry();
    }, 1500);
    tablink='a[href*="#tabs-'+i+'"]';
    $(tablink).click();
}

The timeout is working here, but I don't know why. Here I only have th开发者_如何学Goe problem left with the immediate clicking. It is not waiting until clicking the next tab.

Can somebody Help?

thanks in advance


i totally wrote weird stuff.. sorry... this is my actual code and its working almost:

for (i=0;i<$('#tabs ul .ui-corner-top').size();i++) {
    tablink='a[href*="#tabs-'+i+'"]';
    setTimeout('changeTab(tablink)',i*3000);
}

function changeTab(tablink) {
    $(tablink).click();
}

But he is only clicking the third tab (#tabs-2) 3 times, not every tab once (1,2,3) like i want. For explaining it: I want to switch automatically between tabs (jquery ui tabs) with an interval of waiting until next click.


If I understand what you are asking... you have a number of tabs that you want to progress through with a timer, however your query is being too greedy.

What you will want to do is take your collection invoke each of them with a different offset.

Cheaply, you could do something like this...

    var tabs = $(this).children('a');
    var numberOfTabs = tabs.length;
    for (var item = 0; item < numberOfTabs; item++)
    {
        window.setTimeout(function(){tabs[item].click();},2000*item);
    }

You must remember that JQuery returns an array of results that match your query, AND that timers are not sequentially initialized. A timer declaration starts counting down immediately when you define it. You've merely asked JQuery to start a timer for each result to be activated at the same time.

0

精彩评论

暂无评论...
验证码 换一张
取 消