All timers created in loop (with setTimeout) fire at same time?

后端 未结 3 1388
滥情空心
滥情空心 2020-12-12 06:00

Can someone explain why i cant get the desired delay between each request?
They are all happening at once.

$(window).load(function(){
    $(\'a[href]\').         


        
3条回答
  •  眼角桃花
    2020-12-12 06:24

    Becase setTimeout for all links is called at once. If you want to delays between call you need to do something like this:

     var delay = 2000;
     $('a[href]').each(function(){
        var linkk = $(this)
        var linkkhref = linkk.attr('href');
    
        window.setTimeout(function(){ conectar('HEAD', linkkhref, resp) }, delay);
        delay += 2000;
        ....
    

提交回复
热议问题