代碼可能看起來比它需要更復雜我想將我傳遞到balm函數,但它返回undefined,因爲我正在執行它錯誤。
只是額外的信息:這是我正在寫的遊戲的服務器。在節點的控制檯中運行。我錯誤地將一個變量傳遞給一個函數,但沒有看到我的錯誤
for (i=30;i>=0;i--){
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;
}
});
},(31-i)*333);
}
這裏是一個簡化版本,只是顯示
for (i=3;i>=0;i--){
setTimeout(function foo(i){
console.log(foo)
},1000*i);
我希望2000年以後毫秒 「3」 3000毫秒後經過1000毫秒 「2」 以下輸出 「1」 的概念, 編輯:它的工作時,我定義setTimeout()之外的功能,然後叫它像這樣 setTimeout(香脂(我),...
這就是我剛纔發現感謝您的快速反應。 – Shawn 2012-07-05 21:12:57