0

我有一个服务器在 node.js 中运行一个视频游戏,在控制台中通过 websockets 与客户端交谈。我有一个从 MySQL 数据库生成敌人的功能,但我想这是错误消息,它没有引用我的代码我认为它是一个 jquery 脚本......我想要做的是让脚本定期运行这个函数(对于测试我将其设置为 10 秒(在代码底部设置间隔),但最终我会将其设置为 60 左右。

我可以轻松地设置一个循环和一个 if (new Date().getTime() > event_time) 然后设置 event_time 但我不想在一个恒定循环上耗尽服务器资源。timers.js:223 callback.apply(timer, args); ^ TypeError: 无法在 Timer.ontimeout (timers.js:223:14) 调用未定义的方法“应用”

这是我的代码的重要部分,尽管您基本上不需要遵循其中的大部分内容,但我在底部有一个函数和一个设置间隔方法

function a (){
   sql="SELECT spawns.*, (quantity - COUNT(game_moblist.spawn_id)) AS     quantity_to_spawn,mobs.* FROM spawns LEFT JOIN mobs USING (mob_id) LEFT JOIN game_moblist     USING (spawn_id) GROUP BY spawn_id"
   connection.query(sql, function(err, rows, fields) {
      if (rows[0].quantity_to_spawn>=1){
         ocv = parseInt(rows[0].dex/3)+rows[0].level;
         dcv = parseInt(rows[0].dex/6)+rows[0].level/2;//dex adds same ocv and     dcv+evade combined bonus to make = dex hit about 50%
         evade = parseInt(rows[0].dex/6)+rows[0].level/2;
         dmg = parseInt(rows[0].str/3)+rows[0].level;
         max_hp = rows[0].con * rows[0].level;
         hp=max_hp;
         max_end=rows[0].con * rows[0].level;
         end=rows[0].con * rows[0].level;
         pd = rows[0].level/2;
         ed = rows[0].level/2;
         kill_exp=parseInt((rows[0].kill_exp*(Math.pow(1.25, rows[0].level+1)))*    ((Math.random()*0.1)+1))
         nextturn = new Date().getTime()
         spawn_sql="INSERT INTO game_moblist (posx, posy, ocv, dcv, evade, dmg, hp,     max_hp, end, max_end, pd, ed, land, next_turn, alignment, mob_name, level, spawn_id, mob_id, move_time, attack_time, action_time, kill_exp) VALUES "
         for (i=0;i<rows[0].quantity_to_spawn;i++){
            spawn_sql += "("+rows[0].posx+","+ rows[0].posy+","+ocv+","+dcv+","+evade+","+dmg+","+hp+","+max_hp+","+end+","+max_end+","+pd+","+ed+",'"+rows[0].land+"',"+nextturn+","+rows[0].alignment+",'"+rows[0].mob_name+"',"+rows[0].level+","+rows[0].spawn_id+","+rows[0].mob_id+","+rows[0].move_time+","+rows[0].attack_time+","+rows[0].action_time+","+kill_exp+")";
            if (i<rows[0].quantity_to_spawn-1){
               spawn_sql+=","
             }
         }

          console.log(spawn_sql)
         connection.query(spawn_sql, function(err, rows, fields) {if (err) throw err});
      }
   });
}
setInterval(a(),10000)
4

1 回答 1

0

使用函数指针而不是函数调用作为 setInterval 的第一个参数:

setInterval(a,10000)
于 2012-08-13T21:56:47.603 回答