したがって、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);
}