在 Node.js 中设置计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14868590/
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-09-02 17:09:26  来源:igfitidea点击:

Setting a timer in Node.js

node.jssettimeoutsetinterval

提问by Amanda G

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 秒作为计时器值。任何想法如何进行将非常有帮助。

回答by Marc Fischer

There are basically three ways to go

基本上有三种方式

  1. setInterval()
  1. setInterval()

The setTimeout(f, n)function waits n milliseconds and calls function f. The setInterval(f, n)function calls fevery nmilliseconds.

setTimeout(f, n)函数等待 n 毫秒并调用 function f。该setInterval(f, n)函数fn毫秒调用一次。

setInterval(function(){
  console.log('test');
}, 60 * 60 * 1000);      

This prints testevery 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:

test每小时打印一次。您可以将代码(除了 require 语句)放入setInterval(). 然而,这对我来说似乎有点丑陋。我宁愿去:

  1. Scheduled Tasks Most operating systems have a way of sheduling tasks. On Windows this is called "Scheduled Tasks" on Linux look for cron.

  2. Use a libaryAs I realized while answering, one could even see this as a duplicate of that question.

  1. 调度任务 大多数操作系统都有调度任务的方法。在 Windows 上,这称为“计划任务”,在 Linux 上查找 cron。

  2. 使用库正如我在回答时意识到的那样,人们甚至可以将其视为该问题的重复。