setinterval不通过App-Node.js更新/刷新

时间:2019-01-02 14:12:04

标签: javascript node.js scheduler

我正在尝试编写一个收集主题公园等待时间的程序,以尝试创建一些数据科学实践的数据库。我正在尝试从themeparks node.js包中收集这些数据。

我的背景更多是R和python,所以我正在学习。现在,我可以从指定的公园以所需的正确格式输出.csv文件,文件名中应包含日期,标题为该公园的游乐设施名称,以及包含乘车等待时间的行,并在末尾添加时间戳以指定提取时间的时间。

目前,我正在创建文件,添加了标题,然后每3秒(通常为5分钟,但出于测试目的将其设置为3秒)添加行驶时间行。

我面临的问题是当前,我的时间戳记没有更新,当前时间也不是。

为将来的规划参考,我计划稍后添加一个特定的时间,该时间将根据公园的时间结束并开始计时,但是就目前而言,我将其设置为从应用程序启动起一直运行到无限。另外,我是同一段代码,在同一个应用程序中为其他3个公园运行(猜测哪个公园),以便为它们创建其他.csv,但是当然,它们具有自己的凭据才能获取该数据。后来的目的是让每个公园都有其自己的应用程序,一个主应用程序将要求它们运行,但是现在它们位于同一应用程序中,并且基本上具有与该代码段相同的结构。截至目前,我只需要知道如何获取时间和数据进行更新,以及为什么不更新即可。

// include the Themeparks library
var Themeparks = require("themeparks");
var fs = require('fs');

//Date
var datetime = require('node-datetime');
var dt = datetime.create();
var TodayDate = dt.format('m-d-Y');
var TimeDate = dt.format('m-d-Y H:m');
var TimeStamp = "Time Stamp" //The Header for Time   Stamp




//************************ MAGIC KINGDOM     ***********************

 var disneyMagicKingdom = new     Themeparks.Parks.WaltDisneyWorldMagicKingdom



 disneyMagicKingdom.GetWaitTimes().then(function(rides) {
  for(var i=0 , ride; ride=rides[i++];) {
    // Write out the data in this format: "<RIDE     NAME>",<WAIT TIME>
    fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', "\"" + ride.name + "\"" + ",");
  }
  //Goes to the next line in the csv, so the times will start on the next line.
  fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', TimeStamp);
  fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', '\r\n');

  //Repeats the Wait times interval
  setInterval(MKTime, 1000*3)

  function MKTime() {
    for(var i=0 , ride; ride=rides[i++];) {
      // Write out the data in this format: "<RIDE NAME>",<WAIT TIME>
      fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', ride.waitTime  + ",");
      // Write out a new line so that when this loop repeats, the next row will be written on its own line
    }
    fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', TimeDate);
    //Goes to the next line in the csv so when the next interval starts, the times will be on the next line
    fs.appendFileSync('Magic Kingdom ' + TodayDate + '.csv', '\r\n');
  }
}, console.error);

1 个答案:

答案 0 :(得分:2)

您的日期和时间变量是在代码块的开头创建的,而不是在setInterval调用的函数中创建的。因此,它们将与运行代码时的状态相同,并且不会在多个setInterval调用中进行更改。一个简单的解决方法是将那些变量移到函数MKTime内或在该方法内定义新变量。