我正在开发一个应用程序,其中:
我能够:
这是我的服务器方法:
Meteor.methods({
'tasks.Create'() {
if (!this.userId) {
throw new Meteor.Error('Not authorized.');
}
const id = TasksCollection.insert({
progressionInPercentage:0,
createdAt: new Date(),
userId: this.userId,
});
Meteor.setInterval(()=>{const currentProgressionInPercentage = (ExportsCollection.findOne({_id:id})).progressionInPercentage
ExportsCollection.update({_id:id},{$set:{progressionInPercentage:currentProgressionInPercentage + 5}})},1000)
// can I use the clearInterval
},
Meteor 还提供了一种清除间隔的方法,通过
Meteor.clearInterval
:
Meteor.methods({
'tasks.Create' () {
if (!this.userId) {
throw new Meteor.Error('Not authorized.');
}
const id = TasksCollection.insert({
progressionInPercentage: 0,
createdAt: new Date(),
userId: this.userId,
});
const intervalId = Meteor.setInterval(() => {
const currentProgressionInPercentage = (ExportsCollection.findOne({ _id: id })).progressionInPercentage
if (currentProgressionInPercentage >= 100) {
return Meteor.clearInterval(intervalId)
}
ExportsCollection.update({ _id: id }, { $set: { progressionInPercentage: currentProgressionInPercentage + 5 } })
}, 1000)
}
})
一旦百分比等于或大于 100 在间隔处理函数内,只需清除间隔即可。
资源: