如何在时间范围之间执行一些操作?

时间:2016-05-12 21:06:54

标签: javascript time range

我从API调用中获取时间信息。比如说,输出是:

[
  {
    "ItemID": 7,
    "URL": "http://localhost/img/seven.png",
    "StartTime": "05:00:00"
  },
  {
    "ItemID": 12,
    "URL": "http://localhost/img/twelve.png",
    "StartTime": "14:45:00"
  },
  {
    "ItemID": 16,
    "URL": "http://localhost/img/sixteen.png",
    "StartTime": "17:30:00"
  }
]

我还在我的浏览器中运行一个计时器,它每秒执行一次特定的代码。从API的回调开始的一段时间,比如“14:45:00”到“14:50:00”,持续300秒。

我不确定应该用什么代码来实现它。我已经尝试了一些事情,这一刻发生了,所以,我只是检查时间然后提醒。这就是我现在所拥有的:

setInterval(function () {
  var curDate = new Date();
  var curTime = curDate.getTime();
  /*
    For a range of the time starting from the API's callback,
    say, "14:45:00" till "14:50:00", for 300 seconds.
    I am not sure what should I code to achieve it.
    I already tried something, which happens for that moment
    so, I just check the time and then alert.
  */
  // Moment Code
  $.each(items, function (i, v) {
    var curTimeString = curDate.getHours() + ":" + ("0" + curDate.getMinutes()).slice(-2) + ":" + ("0" + curDate.getSeconds()).slice(-2);
    if (curTimeString == v.StartTime)
      alert("Started!");
  });
  console.log(curTime); // Number of seconds from Jan 1, 1970.
}, 1000);

请指导我如何继续。

0 个答案:

没有答案