NodeJs for循环移动太快

时间:2016-12-23 06:50:23

标签: javascript node.js

我需要帮助解决我的代码中的问题,其中for循环运行得太快并忽略其中的代码。

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);

当这部分代码运行时,我得到

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

当我最终收到的不仅仅是76561198072300099垃圾邮件时。

0 个答案:

没有答案