当我调用Meteor.setTimeout()时,流星保存当前的'this'实例,当时间到时,我调用的函数使用了它的旧实例(当我调用Meteor.setTimeout()时保存的实例)。如何使流星改用“ this”的新实例?
// Ending question
const questionEndToLog = () => this.questionEnd(firstQuestion._id);
Meteor.setTimeout(questionEndToLog, firstQuestion.time * 1000);
questionEnd(qId) {
const addToGameLog = () => {
const questionEnd = new QuestionEnd({
questionId: qId
});
this.gameLog = this.gameLog.concat(questionEnd);
this.save();
return true;
};
const isQuestionEndAlready = !!this.gameLog
.filter(e => e.nameType === eventTypes.QuestionEnd)
.find(e => e.questionId === qId);
return isQuestionEndAlready && addToGameLog();
}
export const QuestionEnd = Class.create({
name: eventTypes.QuestionEnd,
fields: {
nameType: {
type: String,
default() {
return eventTypes.QuestionEnd;
},
},
questionId: {
type: String,
},
createdAt: {
type: Date,
default() {
return new Date();
},
},
},
});
this.gameLog是最旧的日志(调用Meteor.setTimeout()时的日志),而不是新的日志。
最佳答案
那不是流星。当您使用() => ...
时,当前的此上下文将绑定到此函数。