Looping with .each with delay in Jquery

后端 未结 5 1135
时光说笑
时光说笑 2021-01-16 14:20

I am not good at jQuery so I am not sure if my assumptions are corrent.

I am using the isotope plugin, with which I want to insert elements one by one (and not every

5条回答
  •  没有蜡笔的小新
    2021-01-16 15:00

    Because .each() runs pretty instantaneous for each entry, you'll end up with a bunch of timeouts which are more or less the same. So after about 3 seconds, all timeouts expire and the items are added.

    To prevent this, you'll to make the timeout dependent on the index of the item. So item 0 will be inserted after 3 seconds, item 1 will be inserted after 6 seconds, etc.

    $("#items_are_here").find('.item').each(function( index ) {
      var item = $(this);
      setTimeout(function() {
        $('#container').isotope('insert', item);
      },3000 * (index + 1));
    });
    

提交回复
热议问题