【发布时间】:2012-07-05 22:05:35
【问题描述】:
所以我有这个在 setTimeout 上执行的函数,console.log 为我命名为 time 的变量返回正确的值,但是当我运行代码时,该函数正在运行,没有延迟。该代码是我正在制作的游戏的服务器端。该代码应该在使用物品后逐渐恢复玩家的健康。我的问题是 settimeout 函数......当我在浏览器中使用它而不是从我的节点控制台使用它时它也可以工作。
function balm(i){
this_sql ="UPDATE game_moblist SET hp = least(max_hp, hp +"+Math.round(i/2)+") WHERE id ="+mobid;
connection.query(this_sql, function(err, rows, fields) {
if (err) err=null
});
console.log(this_sql);
this_sql = "SELECT hp, max_hp FROM game_moblist WHERE id ="+mobid; //emite catch all update pointint to hp..
connection.query(this_sql, function(err, rows, fields) {
if (err) throw err;
socket.emit ('updatemisc',handler,rows);//eval handler using args
if (rows[0].hp==rows[0].max_hp){
i=0;
return i;
}
});
}
for (i=30;i>=0;i--){
time=(31-i)*1000;
console.log(time);
setTimeout(balm(i),time);
}
【问题讨论】:
标签: javascript node.js