当我希望第一个超时时,间隔会立即刷新

时间:2018-07-10 21:31:53

标签: javascript node.js socket.io

因此,我正在使用一个使用时间间隔的战斗系统,并且在启动该功能时试图阻止其发送垃圾邮件时遇到了问题。它不会创建多个时间间隔,它只会保持刷新状态,如果我停止按与之相关的按钮,它将正常进行。我不确定我要去哪里,这一切对我来说都是正确的。

Updated code below

好的,所以我更新了我的代码,使它不会继续向战斗功能发送垃圾邮件。但是我现在遇到一个问题,它创建了多个setTimeeout()函数:

// FIGHT CALL
  socket.on('fight mob', (mobId) => {
    //socket.emit('mob test', data);
    let sql = 'SELECT * FROM mobs WHERE id = ?';

    // If there is no interval, or if it is false, intiate one.
    if (session.userdata.actionInterval === undefined) {
      db.query(sql, mobId).then(results => {
        session.userdata.actionInterval = setInterval(() => {
          battle.battle(socket, results[0], session);
          session.userdata.intervalTime = Date.now();
        }, 6000);

        session.userdata.intervalTime = Date.now();
        session.userdata.autos = 5;
        battle.battle(socket, results[0], session);
      });
    } else { // If there is a current interval... do this
      var timing = session.userdata.intervalTime + 5900 - Date.now();
      if (timing < 0) timing = 0;
      console.log(timing);

      // Waits for current to end, then updates it.
      db.query(sql, mobId).then(results => {
        setTimeout(() => {
          clearInterval(session.userdata.actionInterval);
          session.userdata.actionInterval = setInterval(() => {
            battle.battle(socket, results[0], session);
            session.userdata.intervalTime = Date.now();
          }, 6000);

          session.userdata.intervalTime = Date.now();
          session.userdata.autos = 5;
          battle.battle(socket, results[0], session);
        }, timing);
      });
    }
  });

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了答案,我怀疑它是最优雅和/或最有效的方法,但目前正在解决问题。我在清除时间间隔方面遇到问题,但我还解决了这一问题……IMO花费的时间太长,但您仍然可以学习。

  // FIGHT CALL
  socket.on('fight mob', (mobId) => {

    let sql = 'SELECT * FROM mobs WHERE id = ?';

    // If there is no interval, or if it is false, intiate one.
    if (session.userdata.actionInterval === undefined || session.userdata.actionInterval === false) {
      db.query(sql, mobId).then(results => {
        session.userdata.actionInterval = setInterval(() => {
          battle.battle(socket, results[0], session);
          session.userdata.intervalTime = Date.now();
        }, 6000);
        session.userdata.intervalTime = Date.now();
        session.userdata.autos = 5;
        battle.battle(socket, results[0], session);
      });
    } else { // If there is a current interval... do this
      var timing = session.userdata.intervalTime + 5900 - Date.now();
      if (timing < 0) timing = 0;

      // Waits for current to end, then updates it.
      db.query(sql, mobId).then(results => {
        if (session.userdata.actionTimeout === undefined) {
          session.userdata.actionTimeout = setTimeout(() => {
            session.userdata.actionInterval = clearInterval(session.userdata.actionInterval);
            session.userdata.actionInterval = setInterval(() => {
              battle.battle(socket, results[0], session);
              session.userdata.intervalTime = Date.now();
            }, 6000);

            session.userdata.intervalTime = Date.now();
            session.userdata.autos = 5;
            battle.battle(socket, results[0], session);
          }, timing);
        }
      });
    }
  });