javascript sleep with SetTimeout

前端 未结 6 2064
粉色の甜心
粉色の甜心 2020-12-21 18:34

I\'m trying to send emails with a 10 seconds delay between. I wrote this code:

$(document).ready(function() {
    for (i = 0; i < 20; i++) {
        setTi         


        
6条回答
  •  甜味超标
    2020-12-21 19:28

    First, pass a function to setTimeout.

    Secondly, you'd be better off if you set the timeout for the next one in the queue after the current one is finished.

    In the for loop:

    sendEmail(0); // start sending first
    

    and in the callback:

          , function(data) {
              if(id < 19) { // if next should be sent
                  setTimeout(function() {
                      SendEmail(id + 1);
                  }, 5000);
              }
              var toAppend = "     " + data + ""
              $("#sentTo").append(toAppend);
          }
    

提交回复
热议问题