简体   繁体   中英

JavaScript console event handling

I have a server running for a video game in node.js in a console speaking via websockets to clients. I have a function to spawn enemies from a MySQL database but I would like to Here is the error message its not refrencing my code I think its a jquery script... What I want to do is have the script periodically run this function (for testing I set it to 10 seconds (set interval at bottom of code) but eventually I'll set it to 60 or so.

I could easily set a loop and an if (new Date().getTime() > event_time) then set event_time but I don't want to use up server resources on a constant loop. timers.js:223 callback.apply(timer, args); ^ TypeError: Cannot call method 'apply' of undefined at Timer.ontimeout (timers.js:223:14)

Heres the significant part of my code though you don't need to follow most of it essentially I have a function and a set interval method at the bottom

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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