Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to get milliseconds in CronJobs?

I want to render JavaScript function in every 500ms using node Cronjobs.

But I could find function for every 1 second as below.

cron.schedule("*/1 * * * * *", function() {
      console.log("running a task every 1 second");
    }); 

Is there anyway to run function on every 500ms using node Cronjob?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Setting 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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading