更新间隔中的值

时间:2018-04-02 04:14:20

标签: javascript node.js discord.js

我试图让函数在下一个时间间隔更新其值。

/*jshint esversion: 6*/
const commando = require('discord.js-commando');
const db = require('../../config/db');

module.exports = class Battle extends commando.Command {
  constructor(client) {
    super(client, {
      name: 'battle',
      aliases: ['fight'],
      group: 'actions',
      memberName: 'battle',
      description: 'What mob to battle.',
      args: [
        {
          key: 'mob',
          prompt: 'What mob do you want to attack? Mob ID only!',
          type: 'integer',
        },
      ],
    });
  }

  run(msg, { mob }) {
    var times = 0;
    var clearBattle = false;
    var mobs = null;
    var userData = msg.member.user; // Set user variable

    var userSQL = 'SELECT * FROM users_meta WHERE userid = ?';
    db.query(userSQL, userData.id, (err, row) => { // User Query
      if (row[0] === undefined) { // If user is not registered, kill process.
        return msg.channel.send('You are not registered!');
      } else {
        userData.game = row[0];
        var mobSQL = 'SELECT * FROM mobs WHERE id = ?';
        db.query(mobSQL, mob, (err, row) => { // Mob Query
          if (row[0] === undefined) { // if mob doesnt exist, kill process.
            return msg.channel.send('That mob doesn\'t exist');
          } else { // If registered and mob exists, start the battle and assign mob
            mobs = row[0];
            if (userData.interval === undefined || userData.interval === false) {
              userData.interval = setInterval(() => {
                battle(mob);
              }, 6000);
              battle(mob);
            } else {
              return mob;
            }
          }



          // Start battle loop function
          function battle(mob) {
            db.query(userSQL, userData.id, (err, row) => {
              userData.game = row[0];
            });

            console.log(mob);
            if (times < 10) {
              msg.channel.send('Six Seconds for ' + userData.username +
              '(' + userData.game.str + ')' + '. Fighting mob ' + mobs.name + '(' + mobs.id + ').');
              times++;
            } else {
              clearInterval(userData.interval);
              userData.interval = false;
            }
          }
        });
      }
    });
  }

};

目前的情况: !战斗1(将战斗说暴徒)

但是,如果我想更改小怪!battle 2,那么它会更改battle()函数内的所有内容的值。我还没有基于mob id的任何计算,所以也许我不太清楚我想要什么。

编辑:我还在苦苦挣扎。我尝试了一切。如果我只是清除间隔并再次创建它会很容易,但我希望它等待重复更新信息。

1 个答案:

答案 0 :(得分:0)

我找到了答案:

    if (userData.interval === undefined || userData.interval === false) {
      userData.interval = setInterval(() => {
        battle(mob);
        userData.intervalTime = Date.now();
      }, 6000);
      battle(mob);
    } else {
      var timing = userData.intervalTime + 5900 - Date.now();
      if (timing < 0) {
        timing = 0;
      }

      setTimeout(() => {
        clearInterval(userData.interval);
        userData.interval = setInterval(() => {
          battle(mob);
          userData.intervalTime = Date.now();
        }, 6000);
        battle(mob);
      }, timing);
    }