简体   繁体   English

Meteor.setTimeout()保存当前的“ this”对象,而不是将来的对象

[英]Meteor.setTimeout() save current 'this' object instead of the future one

When I am calling to Meteor.setTimeout() meteor saves the current instance of 'this' and when time's up the funcion I called uses the old instance of this (the instance that was saved when I call Meteor.setTimeout()). 当我调用Meteor.setTimeout()时,流星保存当前的'this'实例,当时间到时,我调用的函数使用了它的旧实例(当我调用Meteor.setTimeout()时保存的实例)。 How can I cause meteor to use the new instance of 'this' instead ? 如何使流星改用“ 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 is the oldest log (the log that was when the Meteor.setTimeout() called) instead of the new one. this.gameLog是最旧的日志(调用Meteor.setTimeout()时的日志),而不是新的日志。

That's not a Meteor thing. 那不是流星。 When you're using () => ... then the current this context will be bound to this function. 当您使用() => ...当前上下文将绑定到此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM