2012-06-28 93 views
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) 
+2

我不知道是否這是問題,但是您立即調用'a'並將返回值傳遞給'setInterval',它是'undefined'。你必須傳遞一個*函數引用*,所以我想你想'setInterval(a,10000)'。 –

+0

是的我認爲你是對的...謝謝 – Shawn

+1

讓這個答案也許,@FelixKling? –

回答

0

使用function pointer,而不是一個函數調用設定的間隔方法作爲setInterval的第一個參數:

setInterval(a,10000)