Shawn Shawn - 3 months ago 15
Node.js Question

JS - How to correctly use setTimeout()

So I have this function that executes on setTimeout and console.log returns the correct value for the variable I named time however the function is running with no delay when I run the code. The code is server side for a game I am making. The code is supposed to gradually restore health to the player after using an item. My issue is with the settimeout function... also it works when I use it in the browser but not from my node console.

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
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){
return i;
for (i=30;i>=0;i--){


This is the correct usage of setTimeout

setTimeout(balm, time, i);

You have to pass a function, then delay, and then args to the aforementioned function