Jquery等待并点击更多次

发布于 2024-11-05 20:32:50 字数 1074 浏览 3 评论 0 原文

我有一个问题,因为我不明白这一点。

我想在用 jquery 显示下一个选项卡和编码如下之间等待:

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

如果单击下一个选项卡,我预计会看到等待 2 秒。 但超时仅开始一次,所有 href 都会立即单击,无需等待。

然后我尝试了不同的方法:

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();
}

超时在这里起作用,但我不知道为什么。这里我只剩下立即点击的问题。它不会等到单击下一个选项卡。

有人可以帮忙吗?

预先感谢


我完全写了奇怪的东西..抱歉...这是我的实际代码,它几乎可以工作:

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();
}

但他只单击第三个选项卡(#tabs-2)3次,而不是每个选项卡一次(1,2, 3)如我所愿。解释一下:我想在选项卡(jquery ui 选项卡)之间自动切换,并等待下一次单击。

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 the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2024-11-12 20:32:50

如果我明白你在问什么......你有很多选项卡,你想用计时器来完成,但是你的查询太贪婪了。

您要做的就是让您的集合以不同的偏移量调用它们中的每一个。

便宜地,您可以做这样的事情...

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

您必须记住,JQuery 返回与您的查询匹配的结果数组,并且计时器不是按顺序初始化的。当您定义计时器声明时,它会立即开始倒计时。您只是要求 JQuery 为要同时激活的每个结果启动一个计时器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文