Javascript 在未来几天或几个月内执行操作
我正试图找出未来执行任务的最佳方式,例如向用户发送电子邮件 我的想法是在需要发送电子邮件时(与用户数据一起存储在数据库中),每天检查用户需要发送的电子邮件,并使用Meteor的计时器功能Javascript 在未来几天或几个月内执行操作,javascript,email,meteor,futuretask,Javascript,Email,Meteor,Futuretask,我正试图找出未来执行任务的最佳方式,例如向用户发送电子邮件 我的想法是在需要发送电子邮件时(与用户数据一起存储在数据库中),每天检查用户需要发送的电子邮件,并使用Meteor的计时器功能 // 7 hours in millisec. Meteor.setTimeout( function() { Meteor.call( "sendReminderEmail", ... ); }, 2.52e+7 ); 我看到的问题是设置了太多计时器,这会影响性能。什么是好的解决方案 编辑:基本上,
// 7 hours in millisec.
Meteor.setTimeout( function() {
Meteor.call( "sendReminderEmail", ... );
}, 2.52e+7 );
我看到的问题是设置了太多计时器,这会影响性能。什么是好的解决方案
编辑:基本上,我的用例包括用户创建事件,他们将其设置为长期事件或短期事件(基于天、周或月),并根据持续时间接收该事件的后续信息
我想我可以每小时检查一次,但这似乎是一个成本相等的问题。有没有专门针对流星的方法?还是一个更好的概念
Edit2:好的,我意识到准确度对我的问题并不重要,所以我考虑在每个时区设置一个计时器,这样可以发送大量电子邮件。如果用户有一个长期事件,并且他们的提醒是本周,那么现在就发送它。基本上,它取决于事件的持续时间和用户的时区
因此,我最新的问题是,我如何每天运行某个程序,并考虑到我的问题?假设您想在今天上午9点执行代码,现在是上午8点,您可以创建一个超时以匹配目标时间的分钟数,然后创建一个1小时的间隔,并在每次执行时检查时间是否为上午9点,如果是,执行 在这个小规模示例中,当时钟显示9秒时,我正在执行
executeMe()
:
现场测试:
如果你每天都在检查,为什么要使用7小时的超时时间?减少邮件发送次数???@ŠimeVidas这是一天开始的7小时。@Knownasilya我编辑了一个示例,向你展示了如何每1:20执行一个函数,当然你可以为它添加更多的条件,视为example@Knownasilya我修复了两个示例中Date对象的一些错误
<body>
Last run: <span id="time"></span><br>
Next execution: <span id="target"></span>
<script type="text/javascript">
function executeMe(){
alert("9 seconds!");
}
var timeout = null;
var interval = null;
function timer(){
var now = new Date();
document.getElementById('time').innerHTML = now;
document.getElementById('target').innerHTML = new Date(now.getTime()+ 1000);
//console.log("timer()", now);
if(now.getSeconds() == 9)
setTimeout("executeMe();",1); // async
if(interval == null)
interval = setInterval("timer()",1000);
}
var now = new Date();
var target = new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds()+1,0);
//console.log("now", now);
//console.log("target", target);
//console.log("diff", target.getTime() - now.getTime());
document.getElementById('target').innerHTML = target;
timeout = setTimeout("timer()", target.getTime() - now.getTime() );
</script>
<body>
Last run: <span id="time"></span><br>
Next execution: <span id="target"></span>
<script type="text/javascript">
function executeMe(){
alert("1:20am!");
}
var timeout = null;
var interval = null;
function timer(){
var now = new Date();
document.getElementById('time').innerHTML = now;
document.getElementById('target').innerHTML = new Date(now.getTime()+ 1*60*60*1000);
//console.log("timer()", now);
if(now.getHour() == 1)
setTimeout("executeMe();", 20*60*1000); // !!!! this will execute at 1:20am
if(interval == null)
interval = setInterval("timer()",1*60*60*1000); // !!!! repeat every hour
}
var now = new Date();
// !!!! targeting next exact hour
var target = new Date(now.getFullYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes()+1,0,0);
//console.log("now", now);
//console.log("target", target);
//console.log("diff", target.getTime() - now.getTime());
document.getElementById('target').innerHTML = target;
timeout = setTimeout("timer()", target.getTime() - now.getTime() );
</script>
</body>