繁体   English   中英

JavaScript控制台事件处理

[英]JavaScript console event handling

我在通过websockets向客户端讲话的控制台中的node.js中运行着一个视频游戏服务器。 我有一个从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)调用未定义的方法'apply'

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

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)

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

setInterval(a,10000)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM