admin管理员组文章数量:1326081
I want to trigger a JavaScript function every 500ms using node cron jobs, though I couldn't find a way to make the cron execute any lower than a 1 second interval.
cron.schedule("*/1 * * * * *", function() {
console.log("running a task every 1 second");
});
Is there any way to run a function every 500ms using node cron jobs?
I want to trigger a JavaScript function every 500ms using node cron jobs, though I couldn't find a way to make the cron execute any lower than a 1 second interval.
cron.schedule("*/1 * * * * *", function() {
console.log("running a task every 1 second");
});
Is there any way to run a function every 500ms using node cron jobs?
Share Improve this question edited Jun 26, 2023 at 15:19 Skully 3,1263 gold badges27 silver badges41 bronze badges asked Nov 25, 2021 at 6:05 KavishkaKavishka 2513 silver badges11 bronze badges 5- If you need a task to occur that frequently, cron jobs are the wrong tool. – Skully Commented Nov 25, 2021 at 6:06
- @Skully Then which tool I can use for? – Kavishka Commented Nov 25, 2021 at 6:07
- Why not just use a Node.js timer? – Skully Commented Nov 25, 2021 at 6:08
- @Skully Like setInterval? – Kavishka Commented Nov 25, 2021 at 6:09
- I have posted an answer with an example on how to achieve this, let me know if it meets what you're trying to do. – Skully Commented Nov 25, 2021 at 6:11
1 Answer
Reset to default 8Setting a cron job to execute that frequently is not possible, and for good reason - a task executing that frequently shouldn't be done using a cron.
Instead, you can use Timers with Node.js:
function myFunc(arg) {
console.log("Argument received: " + arg);
}
setTimeout(myFunc, 500, "some message"); // Executes every 500ms.
Timers can also be instantiated into a variable:
const timeoutObject = setTimeout(() => {
console.log("I will print every 500ms!");
}, 500);
clearTimeout(timeoutObject);
本文标签: javascriptIs there any way to get milliseconds in CronJobsStack Overflow
版权声明:本文标题:javascript - Is there any way to get milliseconds in CronJobs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195398a2430986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论