0
我有一台服务器在node.js中的一个视频游戏中通过websockets与客户端通话。我有一个函数来从MySQL数据库中产生敌人,但我想 这是错误消息,它没有折叠我的代码我认为它是一个jQuery脚本... 我想要做的是让脚本定期运行此功能(测试我把它设置为10秒(设置间隔底部的代码),但最终我会将它设置为60左右JavaScript控制台事件处理
我可以很容易地设置一个循环和一个if(new Date()。getTime )> event_time),然后设置event_time,但我不想在一个恒定的循环中耗尽服务器资源 timers.js:223 callback.apply(timer,args); ^ TypeError:Can not call method'apply'未定义 at 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)
我不知道是否这是问题,但是您立即调用'a'并将返回值传递给'setInterval',它是'undefined'。你必须传递一个*函数引用*,所以我想你想'setInterval(a,10000)'。 –
是的我认为你是对的...谢谢 – Shawn
让这个答案也许,@FelixKling? –