Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor个人用户计时器-setInterval()_Meteor - Fatal编程技术网

Meteor个人用户计时器-setInterval()

Meteor个人用户计时器-setInterval(),meteor,Meteor,我正在使用Meteor建立一个在线测验系统。测验中的每一次使用都应将自己的计时器设置为一小时。计时器应该在用户首次登录时启动。 我目前正在维护一个名为UserClocks(userId,timeLeft)的集合 我写了一个像这样的设置间隔 var interval=Meteor.setInterval(函数(){ log(“用户id:+this.userId”); log(“搜索时钟:“+clocksMap[this.userId]); //clocksMap包含用户时钟的_id。 var us

我正在使用Meteor建立一个在线测验系统。测验中的每一次使用都应将自己的计时器设置为一小时。计时器应该在用户首次登录时启动。 我目前正在维护一个名为UserClocks(userId,timeLeft)的集合

我写了一个像这样的设置间隔

var interval=Meteor.setInterval(函数(){
log(“用户id:+this.userId”);
log(“搜索时钟:“+clocksMap[this.userId]);
//clocksMap包含用户时钟的_id。
var userClock=UserClocks.find({u id:clocksMap[this.userId]});
如果(!!用户时钟){
log(“找到的时钟:+userClock.timeLeft”);
}否则{
console.log(“未找到用户的时钟:“+this.userId”);
}
var time=userClock.timeLeft;
时间=时间-1;
更新(userClock.\u id,{timeLeft:time});
log(“更新的userClock.timeleft:+time”);
如果(时间==0){
//注销用户和清除间隔
clearInterval(intervalMap[this.userId]);
}
}, 1000);
此setInterval()位于一个名为b的用户方法中。 由于用户时钟未定义,我不断得到时间限制。
我想我理解为什么不能在setInterval()中使用this.userId,但我需要一个解决问题的方法。如何为每个用户单独设置计时器

将此代码放入
Meteor.methods
块中。您可以在
setInterval
函数中使用
Meteor.userId()
,但不能使用
this.userId
。在回调中,
可能会设置为
窗口
,该窗口没有
用户ID

如果您在Meteor.publish回调中运行此操作,请事先存储
用户ID

var userId = this.userId;

Meteor.setInterval(function() {
    clock = UserClocks.find({_id: userId});
    // Do stuff
}

如上所述,我建议的另一件事是将文档存储在
UserClocks
中,使用
\u id
userId
对应于
Meteor.userId()
。在集合之外保留单独的映射是没有意义的。

我还尝试了从方法参数到.find()的userId。它仍然返回未定义的timeLeft!如果用户在控制台中输入UserClocks.update(userClock.\u id,{timeLeft:999999}),该怎么办?我尝试了,但没有定义。但是,UserClocks.find({u id:{o8y5nw4Wp5H7SF8bP”)返回了LocalCollection.Cursor。我正在使用不安全的包,并已将我的UserClocks放在model.js中进行共享。我尝试了您的建议。userId在setInterval()内外都可以正常工作。我还尝试将其作为参数传递给该方法。此setInterval已在Meteor.methods块中的方法中。尝试了您建议的方法后,我仍将timeLeft和clockId设置为未定义。输出:CLock id:5EzgSavncgBxR8QJ I20140125-02:10:03.546(5.5)用户id:JFPfgyeordrME6m4M I20140125-02:10:03.547(5.5)?搜索时钟:5EZGSAVNCGBXR8QJ I20140125-02:10:03.547(5.5)?找到未定义的时钟I20140125-02:10:03.547(5.5)?找到时钟:未定义的I20140125-02:10:03.553(5.5)?更新的用户时钟。timeleft:NaN I20140125-02:10:04.551(5)?用户id:JFPFGYE4M