Reputation: 25
I am using Meteor.setInterval to count up to keep track of a players "action points" in a game. When they use an action (like attacking), it will decrease the amount. However, when an event is triggered(attacking) it updates players profile on my DB which triggers the setInterval to stack each time the DB is updated. It starts to counter faster and faster. edit This is the issue. I don't want it to count faster. I want only one interval to be running.
This was the closest solution I could find: prevent javascript setInterval function stacking up
Didn't quite work and I couldn't find a different way to arrange the flags to only have one setInterval going.
Heres my helper: Each player has a different regen which is pulled from their profile. I've substituted fixed numbers for the interval and did a Session.get() when the stacking occurs, it stays the same. Instead of numbers I was just adding 'x' so it looks like a loading bar.
apTicker:function() {
Session.set('apRegen', Meteor.user().profile.apRegen);
Meteor.setInterval(function () {
if (Session.get('ap').length < 25) {
Session.set('ap', Session.get('ap')+"x");
}
}, Session.get('apRegen'))
},
Thank you.
Upvotes: 1
Views: 428
Reputation: 7139
This should do what you want:
var apTickerInterval; //To keep track of the interval
Session.set('apRegen', Meteor.user().profile.apRegen);
//...
apTicker : function()
{
if(apTickerInterval)
Meteor.clearInterval(apTickerInterval);
apTickerInterval = Meteor.setInterval(function () {
if (Session.get('ap').length < 25) {
Session.set('ap', Session.get('ap')+"x");
}
}, Session.get('apRegen'));
}
Upvotes: 1