NodeJs for loop moving too fast

330
December 23, 2016, at 7:13 PM

I need help solving a problem in my code where the for loop runs too fast and disregards the code inside it.

setTimeout(function() {
    SendPHtml = "";
    for (var e = CurrentGame; e >= CurrentGame - 4; e--) {
        //console.log(d); // acutally does work
        (function () {
            var k = e;
            connection.query("SELECT * FROM `game" + e + "` ORDER BY id;", function(err, rows, fields) {
                if (err) throw err;
                SteamIdCheck = "";
                ItemsAdd = 0;
                ItemCostT = 0;
                SendPHtml += '<div class="round">';
                //console.log(k);
                //console.log(CurrentGame);
                if(CurrentGame != k) {
                    SendPHtml += '<div class="winner"><div class="winner-background"></div><div class="winner-wrapper">';
                    SendPHtml += '<p class="winner-text text-center"><img src="' + ServerWidePData[GameHistory[k].userid].avatar + '" alt="" rel="nofollow"> ' + ServerWidePData[GameHistory[k].userid].name + ' won $' + GameHistory[k].cost + ' with a chance of ' + GameHistory[k].percent + '%</p>';
                    SendPHtml += '<p class="winner-info text-left">Total Items: ' + GameHistory[k].itemsnum + ' | Percentage: ' + GameHistory[k].module + '%</p></div></div>';
                }
                for (var d = 0; d < rows.length; d++) {
                    (function () {
                        var gg = d;
                        if(gg == 0) {
                            ItemsAdd = 0;
                            ItemCostT = 0;
                            ItemCostT = ItemCostT + rows[gg].cost;
                            SteamIdCheck = rows[gg].userid;
                            //console.log("Clean: " + SteamIdCheck);
                            AddStart = true;
                            SendPHtml += '<div class="bet"> <table class="table-bet"><tbody><tr> <td class="level hidden-xs hidden-sm">';
                            SendPHtml += '<span>' + ServerWidePData[rows[gg].userid].level + '</span> </td> <td class="icon"> <img src="#AVATARIMG" alt="Icon" rel="nofollow" style="border-bottom:3px solid #955ad3;">';
                            SendPHtml += '</td> <td>Icessss deposited PENISCABOTAGE skins valued at $35.46</td> <td onclick="DisplayItems(\'citems\')" class="time"><i class="fa fa-arrow-down"></i></td>';
                            SendPHtml += '</tr></tbody></table><div id="citems" class="items" style="display: none;"> ';
                        }
                        if(rows[gg].userid == SteamIdCheck) {
                            ItemsAdd++;
                            console.log("Clean: " + SteamIdCheck);
                            ItemCostT = ItemCostT + rows[gg].cost;
                            SendPHtml += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">';
                            SendPHtml += '<div class="item"> <img src="#ITEMPICTURE" alt="" class="img-responsive" rel="nofollow" width="120" height="100"> <div class="price">$2</div>';
                            SendPHtml += '<div class="name">' + rows[gg].item + '</div> </div> </div>';
                        } else {
                            SteamIdCheck = rows[gg].userid;
                            ItemCostT = ItemCostT + rows[gg].cost;
                            if(AddStart) {
                                SendPHtml = SendPHtml.replace("PENISCABOTAGE", ItemsAdd);
                                AddStart = false;
                            }
                            ItemsAdd = 0;
                            ItemsAdd++;
                            SendPHtml += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">';
                            SendPHtml += '<div class="item"> <img src="#ITEMPICTURE" alt="" class="img-responsive" rel="nofollow" width="120" height="100"> <div class="price">$2</div>';
                            SendPHtml += '<div class="name">StatTrakā„¢ USP-S | Torque (Minimal Wear)</div> </div> </div>';
                            SendPHtml += '<div class="clearfix"></div> </div> </div>';
                            SendPHtml += '<div class="bet"> <table class="table-bet"><tbody><tr> <td class="level hidden-xs hidden-sm">';
                            SendPHtml += '<span>' + ServerWidePData[rows[gg].userid].level + '</span> </td> <td class="icon"> <img src="#AVATARIMG" alt="Icon" rel="nofollow" style="border-bottom:3px solid #955ad3;">';
                            SendPHtml += '</td> <td>Ice deposited ' + ItemsAdd + ' skins valued at $35.46</td> <td onclick="DisplayItems(\'citems\')" class="time"><i class="fa fa-arrow-down"></i></td>';
                            SendPHtml += '</tr></tbody></table><div id="citems" class="items" style="display: none;"> ';
                        }
                        if( gg == rows.length - 1 ) {
                            SendPHtml += '<div class="clearfix"></div> </div> </div>';
                        }
                    }());
                }
                SendPHtml += '<div class="new"><h3>New round started</h3><h4>Game: ' + k + '</h4></div>';
                SendPHtml += '</div>';
            });
        }());
    }
    console.log("Current player list...");
}, 2000);

When this part of the code runs i get

Clean: 76561198100159702
Clean: 76561198062051242
Clean: 76561198072300099
Clean: 76561198072300099
Clean: 76561198072300099
Clean: 76561198072300099
Clean: 76561198072300099
Clean: 76561198072300099
Clean: 76561198072300099

When i should be getting more than just 76561198072300099 spammed at the end.

READ ALSO
Equivalent JQuery for Javascript id passing

Equivalent JQuery for Javascript id passing

I know the javascript to pass id when an event is triggered as below

372
Which REST API is used to populate countries, state and cities using javascript [on hold]

Which REST API is used to populate countries, state and cities using javascript [on hold]

In my web application,which code and API can i use to populate country,state,city

328
How to change from keyboard events to mouse events

How to change from keyboard events to mouse events

The code below been programmed to do keyboard event's listening , but I want to change it to listen to mouse buttonsFor example, Instead of feeding fast with Q I want it to be with right mouse button, instead of split with space i want it to be with left mouse button,...

338
javascript Close button not working

javascript Close button not working

I am trying to close my modal, but it is not closingIf I click on button to close modal, the whole page reloads

323