通过集合setInterval解析Meteor只能工作一次

时间:2015-07-26 06:05:10

标签: javascript meteor

所以我试图多次检查一个集合中的某些东西。在页面加载一切都很好,但是当setInterval启动并尝试查看数据时,第二次它的行为就像一个数据的和平是空的,无法找到任何信息。

Template.something.helpers({
    repeatCheck: function repeatCheck() {
    setInterval(findTechs, 5000);
    console.log("repeatcheck");
  }
});

function findTechs() {
    _Queue.find({}).forEach(checkTime(this));
  };

  function checkTime(techInfo) {
  console.log(newTime);
  console.log(techInfo);
  console.log(techInfo.endTime < newTime);
  if (techInfo.endTime < newTime) {
    console.log("fun");
    if (_Queue.findOne({
        _id: techInfo._id
      })) {
      _Queue.remove({
        _id: techInfo._id
      })
    }
  }
};

1 个答案:

答案 0 :(得分:0)

你的JS出现了错误。

function findTechs() {
  _Queue.find({}).forEach(checkTime(this));
};

这将以当前上下文作为参数调用checkTime一次。 this可能在此引用window对象。在JavaScript中,当包含函数的变量后面有括号时,会进行函数调用。因此,checkTime(this)会调用checkTime,然后返回对checkTime的调用将返回的内容。然后使用该值(cursor.forEach)调用undefined。因此,光标内的文档将使用undefined进行迭代,而_Queue.find({}).forEach(checkTime); 则无效。

你想要做的是这样的事情:

checkTime

此处cursor.forEach变量背后的实际功能会传递给Location来电。

您可能想要阅读JavaScript回调。