I need to run code in Node.js every 24 hours. I came across a function called setTimeout
. Below is my code snippet
我需要每24小时在Node.js中运行代码。我遇到了一个名为setTimeout的函数。下面是我的代码片段
var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
var data='<?xml version="1.0"?><entries><entry><TenantId>12345</TenantId><ServiceName>MaaS</ServiceName><ResourceID>enAAAA</ResourceID><UsageID>550e8400-e29b-41d4-a716-446655440000</UsageID><EventType>create</EventType><category term="monitoring.entity.create"/><DataCenter>global</DataCenter><Region>global</Region><StartTime>Sun Apr 29 2012 16:37:32 GMT-0700 (PDT)</StartTime><ResourceName>entity</ResourceName></entry><entry><TenantId>44445</TenantId><ServiceName>MaaS</ServiceName><ResourceID>enAAAA</ResourceID><UsageID>550e8400-e29b-41d4-a716-fffffffff000</UsageID><EventType>update</EventType><category term="monitoring.entity.update"/><DataCenter>global</DataCenter><Region>global</Region><StartTime>Sun Apr 29 2012 16:40:32 GMT-0700 (PDT)</StartTime><ResourceName>entity</ResourceName></entry></entries>'
etree = et.parse(data);
var t = process.hrtime();
// [ 1800216, 927643717 ]
setTimeout(function () {
t = process.hrtime(t);
// [ 1, 6962306 ]
console.log(etree.findall('./entry/TenantId').length); // 2
console.log('benchmark took %d seconds and %d nanoseconds', t[0], t[1]);
//benchmark took 1 seconds and 6962306 nanoseconds
},1000);
I want to run the above code once per hour and parse the data. For my reference I had used one second as the timer value. Any idea how to proceed will be much helpful.
我想每小时运行一次上面的代码并解析数据。作为我的参考,我使用了一秒作为计时器值。任何想法如何继续将是非常有帮助的。
1 个解决方案
#1
39
There are basically three ways to go
基本上有三种方法可循
-
setInterval()
- 的setInterval()
The setTimeout(f, n)
function waits n milliseconds and calls function f
. The setInterval(f, n)
function calls f
every n
milliseconds.
setTimeout(f,n)函数等待n毫秒并调用函数f。 setInterval(f,n)函数每隔n毫秒调用f。
setInterval(function(){
console.log('test');
}, 60 * 60 * 1000);
This prints test
every hour. You could just throw your code (except the require statements) into a setInterval()
. However, that seems kind of ugly to me. I'd rather go with:
每小时打印测试一次。您可以将代码(require语句除外)抛出到setInterval()中。然而,这对我来说似乎有些难看。我宁愿一起去:
-
Scheduled Tasks Most operating systems have a way of sheduling tasks. On Windows this is called "Scheduled Tasks" on Linux look for cron.
计划任务大多数操作系统都有调度任务的方法。在Windows上,这在Linux上称为“计划任务”,代表cron。
-
Use a libary As I realized while answering, one could even see this as a duplicate of that question.
使用图书馆正如我在回答时意识到的那样,人们甚至可以将其视为该问题的副本。
#1
39
There are basically three ways to go
基本上有三种方法可循
-
setInterval()
- 的setInterval()
The setTimeout(f, n)
function waits n milliseconds and calls function f
. The setInterval(f, n)
function calls f
every n
milliseconds.
setTimeout(f,n)函数等待n毫秒并调用函数f。 setInterval(f,n)函数每隔n毫秒调用f。
setInterval(function(){
console.log('test');
}, 60 * 60 * 1000);
This prints test
every hour. You could just throw your code (except the require statements) into a setInterval()
. However, that seems kind of ugly to me. I'd rather go with:
每小时打印测试一次。您可以将代码(require语句除外)抛出到setInterval()中。然而,这对我来说似乎有些难看。我宁愿一起去:
-
Scheduled Tasks Most operating systems have a way of sheduling tasks. On Windows this is called "Scheduled Tasks" on Linux look for cron.
计划任务大多数操作系统都有调度任务的方法。在Windows上,这在Linux上称为“计划任务”,代表cron。
-
Use a libary As I realized while answering, one could even see this as a duplicate of that question.
使用图书馆正如我在回答时意识到的那样,人们甚至可以将其视为该问题的副本。