最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - trigger('click') inside setInterval() - Stack Overflow

programmeradmin1浏览0评论

i have this code:

setInterval(function() {
    $('.button2').trigger('click');
    $('.button3').trigger('click');
}, 5000);

result:

it presses .button2 and .button3 at the same time every 5 seconds. I need something like this - 5 seconds passed press .button2, 5 seconds passed press .button3, need a delay between each button clicks and when .button3 is pressed - again press .button2 (something like a loop).

Any help would be appreciated!

EDIT for flesk:

$(document).ready(function (){
    $('.goluboi a').click(function(){
        var integer = $(this).attr('rel');
        $('.videos .cover').animate({left:-875*(parseInt(integer)-1)})  /*----- Width of div mystuff (here 160) ------ */
        $('.goluboi a').each(function(){
        $(this).removeClass('active');
            if($(this).hasClass('button'+integer)){
                $(this).addClass('active')}
        });
    });

    setTimeout(function() {
        triggerClick('.button', 0)
    }, 5000);
    function triggerClick(selector, index) {
        var buttons = $(selector);
        var mod = index++ % buttons.length;
        $(buttons[mod]).trigger('click');
        setTimeout(function() {
            triggerClick(selector, index);
        }, 5000);
    }
});

i have this code:

setInterval(function() {
    $('.button2').trigger('click');
    $('.button3').trigger('click');
}, 5000);

result:

it presses .button2 and .button3 at the same time every 5 seconds. I need something like this - 5 seconds passed press .button2, 5 seconds passed press .button3, need a delay between each button clicks and when .button3 is pressed - again press .button2 (something like a loop).

Any help would be appreciated!

EDIT for flesk:

$(document).ready(function (){
    $('.goluboi a').click(function(){
        var integer = $(this).attr('rel');
        $('.videos .cover').animate({left:-875*(parseInt(integer)-1)})  /*----- Width of div mystuff (here 160) ------ */
        $('.goluboi a').each(function(){
        $(this).removeClass('active');
            if($(this).hasClass('button'+integer)){
                $(this).addClass('active')}
        });
    });

    setTimeout(function() {
        triggerClick('.button', 0)
    }, 5000);
    function triggerClick(selector, index) {
        var buttons = $(selector);
        var mod = index++ % buttons.length;
        $(buttons[mod]).trigger('click');
        setTimeout(function() {
            triggerClick(selector, index);
        }, 5000);
    }
});
Share Improve this question edited Nov 7, 2011 at 15:09 Alexander Kim asked Nov 7, 2011 at 10:17 Alexander KimAlexander Kim 18.4k23 gold badges107 silver badges165 bronze badges 1
  • Same here. You're lucky you got this many answers Heihachi. – flesk Commented Nov 7, 2011 at 13:38
Add a ment  | 

4 Answers 4

Reset to default 3

You could do:

setTimeout(function() {
    triggerClick('.button2');
}, 5000);

function triggerClick(selector) {
    $(selector).trigger('click');
    selector = (selector == '.button2') ? '.button3' : '.button2';
    setTimeout(function() {
        triggerClick(selector);
    }, 5000);
}

EDIT: Answer to your ment below:

setTimeout(function() {
    triggerClick('.button', 0)
}, 5000);

function triggerClick(selector, index) {
    var buttons = $(selector);
    var mod = index++ % buttons.length;
    $(buttons[mod]).trigger('click');
    setTimeout(function() {
        triggerClick(selector, index);
    }, 5000);
}

Works for any number of buttons, as long as they all have the same class. There's no need to call them 'button1', 'button2' and 'button3' anyway. That's what id's are for.

EDIT2:

If I understand your update correctly, what you want is:

setTimeout(function() {
    triggerClick('.goluboi a.button', 0)
}, 5000);
function triggerClick(selector, index) {
    var buttons = $(selector);

    // Your logic
    var integer = $(this).attr('rel');
    $('.videos .cover').animate({left:-875*(parseInt(integer)-1)})
    $(buttons).removeClass('active');

    var mod = index++ % buttons.length;

    $(buttons[mod]).addClass('active').trigger('click');
    setTimeout(function() {
        triggerClick(selector, index);
    }, 5000);
}
});

you could do this with setTimeout

function a() {
    $('.button2').trigger('click');
    setTimeout(b, 5000);
}
function b() {
    $('.button3').trigger('click');
    setTimeout(a, 5000);
}
setTimeout(a, 5000);

Ugly but it works.

var clickButton = false;

setInterval(function() {
    if( clickButton ) {
        clickButton = false;
        $('.button2').trigger('click');
    } else {
        clickButton = true;
        $('.button3').trigger('click');
    }
}, 5000);

Good answers already posted. Here's another:

setInterval(function() {
    $('.button2').trigger('click');
}, 10000);

setTimeout(function() {
    setInterval(function() {
        $('.button3').trigger('click');
    }, 10000);
}, 5000);

This starts the first interval immediately and the second interval after 5 seconds, with the end result that two intervals are running every 10 seconds, but offset by 5 seconds.

发布评论

评论列表(0)

  1. 暂无评论