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

后端 未结 3 1391
滥情空心
滥情空心 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:19

    The loop is run just instantly and delay every execution of conectar function for 2000ms from the time code is exectued.

    For simple case I'd use:

    $('a[href]').each(function(idx){
        ...
        window.setTimeout(function(){ conectar('HEAD', linkkhref, resp) }, idx*2000)
    

提交回复
热议问题