setTimeout inside $.each()

前端 未结 4 1830
借酒劲吻你
借酒劲吻你 2021-02-07 13:02

ok, so I\'ve got this code:

$(this).find(\'article.loading\').each( function(i) {

    var el = this;
        setTimeout(function () {
        $(el).replaceWith(         


        
4条回答
  •  一个人的身影
    2021-02-07 13:59

    It's exactly how Andy McCluggage written. I think something like this could help you.

    var speed = 1000;
    
    // init timer and stores it's identifier so it can be unset later
    var timer = setInterval(replaceArticle, speed);
    
    var articles =  $('article.loading');
    var length = articles.length;
    
    var index = 0;
    function replaceArticle() {
         articles.eq(index).replaceWith($('#dumpster article:first'));
    
         index++;
    
         // remove timer after interating through all articles
         if (index >= length) {
             clearInterval(timer);
         }
    }
    

提交回复
热议问题