1

I want this function to initiate every 4 seconds. is there a way of putting them in an array and call it every 4 seconds rather than this way. Also, this way has a delay of 16 seconds before it actually begins. Thanks in advance.

setInterval(function() {
     setTimeout(function () {
         $j('.menu2').trigger('click');
         }, 0000);

         setTimeout(function () {
         $j('.menu3').trigger('click');
          }, 4000);

         setTimeout(function () {
         $j('.menu4').trigger('click');
          }, 8000);

         setTimeout(function () {
         $j('.menu1').trigger('click');
          }, 12000);

}, 16000);
4

2 回答 2

1

试试这个

setInterval(function () {
        var time = 0;
        for (i = 1; i < 5; i++) {
            setTimeout(function () {
                $j('.menu' + i).trigger('click');
            }, time);
            time += 4000;
        }
    }, 16000);

更新

http://jsfiddle.net/6NwLG/1/

setInterval(function () {
        var time = 0;
        for (i = 1; i < 5; i++) {

            test(time, i);
            time += 4000;
        }
        function test(time, id) {
            setTimeout(function () {
                $('.menu' + id).click();
            }, time);
        }
    }, 16000);
于 2012-12-13T12:52:17.957 回答
0
$(document).delay(12000, function() {
    var interval = setInterval( function() {
       time = 2000;
       m = 1;
        for( i =0; i < 5; i++) {
       setTimeout( function() {
           $('.menu'+m).trigger('click');
           time = time+2000;
           m = m+1;
       }, time);
    }
    }, 4000);
});

​</p>

于 2012-12-13T13:08:46.507 回答