How do I use Node.js to send an email every 10 seconds?

后端 未结 1 1317
[愿得一人]
[愿得一人] 2021-02-14 10:28

I\'m not sure what\'s the best way to add a delay of 10 seconds.

setTimeouts don\'t work, I\'m not sure...

In python, I\'m used to doing \"time.sleep\"

I

相关标签:
1条回答
  • 2021-02-14 10:56

    setTimeout will work but you have to recreate the timeout at the end of each function call.

    You'd do that like this.

    function sendEmail() {
      email.send(to, headers, body);
      setTimeout(sendEmail, 10*1000);
    }
    setTimeout(sendEmail, 10*1000);
    

    What you probably want is setInterval.

    function sendEmail() {
       email.send(to, headers, body);
    }
    setInterval(sendEmail, 10*1000);
    
    0 讨论(0)
提交回复
热议问题