javascript 如何使用 Node.js 每 10 秒发送一封电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16978674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:39:54  来源:igfitidea点击:

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

javascriptpythonnode.js

提问by TIMEX

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

我不确定添加 10 秒延迟的最佳方法是什么。

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

setTimeouts 不起作用,我不确定...

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

在python中,我习惯于做“time.sleep”

I'm not asking for how to send an email. I'm asking how to execute a command every 10 seconds.

我不是问如何发送电子邮件。我在问如何每 10 秒执行一次命令。

回答by Daniel

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

setTimeout将工作,但您必须在每个函数调用结束时重新创建超时。

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.

你可能想要的是setInterval.

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