延迟循环每次应运行5次

时间:2016-05-18 09:52:35

标签: javascript loops

我的这个功能每隔10秒运行一次,它应该这样做,直到j = 50。 它工作得很好。 但我想要的是,在每个间隔,一些东西被执行5次,5个被添加到j。 第一个循环按原样执行,但接下来的2个不是并且不会接近执行代码5次但更多然后停止。

不确定我是否足够清楚,但也许看一下代码会告诉你我做错了什么?

我的代码:

function botsLoop() {          
    setTimeout(function () {
        for (var startloop = 0; startloop <= 5;startloop++) {
            var dateTime = new Date();  
            ajaxChat.messagedata = ({
                "messageinfo": {
                    "userID": botUserNodes[j].userID,
                    "userName": botUserNodes[j].userName,
                    "userRole": botUserNodes[j].userRole,
                    "UserGender": botUserNodes[j].UserGender,
                    "Profile_Provider": botUserNodes[j].ProfilePro,
                    "Profile_user_name": botUserNodes[j].Profilename,
                    "UserRegion": botUserNodes[j].nameregion,
                    "channelID": botUserNodes[j].channel,
                    "istor": botUserNodes[j].IsTor,
                    "channelName": botUserNodes[j].channelName,
                    "text": 'userenterinchat,' + botUserNodes[j].lat + ', ' + botUserNodes[j].lng + ', ' + botUserNodes[j].namecity + ', ' + botUserNodes[j].nameregion,
                    "dateTime": dateTime
                }
            });

            Onlinelistupdate = {
                "onlinelist": {
                    "userID": botUserNodes[j].userID,
                    "userName": botUserNodes[j].userName,
                    "UserGender": botUserNodes[j].UserGender,
                    "Profile_Provider": botUserNodes[j].ProfilePro,
                    "Profile_user_name": botUserNodes[j].Profilename,
                    "Profile_user_picture": botUserNodes[j].profilepicture,
                    "UserRegion": botUserNodes[j].nameregion,
                    "channelID": botUserNodes[j].channel,
                    "istor": botUserNodes[j].IsTor,
                    "channelName": botUserNodes[j].channelName,
                    "camonoff": botUserNodes[j].camonoff,
                    "ABOUTME": botUserNodes[j].ABOUTME,
                    "lat": botUserNodes[j].lat,
                    "lng": botUserNodes[j].lng,
                    "namecity": botUserNodes[j].namecity,
                    "nameregion": botUserNodes[j].nameregion,
                    "ismobile": botUserNodes[j].ismobile,
                    "namecountry":  botUserNodes[j].namecountry
                }
            };   

            sendBotMsg(ajaxChat.messagedata, ajaxChat.channelID,Onlinelistupdate);      
            j++;   

            if (j < 50) {          
                botsLoop();            
            }

        }
        startloop = 0;
    }, 10000)
}

溶液

    function botsLoop() {
              setTimeout(function () {
                if (j < 50) {                     
                  var times = 5;
                  for (var i=0; i < times; i++){
                    console.log('inner loop: ' + (i + 1));
                    j++;
             var dateTime = new Date();  
             ajaxChat.messagedata = ({"messageinfo":{ "userID": botUserNodes[j].userID, "userName": botUserNodes[j].userName, "userRole": botUserNodes[j].userRole,
              "UserGender": botUserNodes[j].UserGender, "Profile_Provider": botUserNodes[j].ProfilePro, "Profile_user_name": botUserNodes[j].Profilename, "UserRegion": botUserNodes[j].nameregion,
             "channelID": botUserNodes[j].channel, "istor": botUserNodes[j].IsTor, "channelName": botUserNodes[j].channelName, "text": 'userenterinchat,' + botUserNodes[j].lat + ', ' + botUserNodes[j].lng + ', ' + botUserNodes[j].namecity + ', ' + botUserNodes[j].nameregion, "dateTime": dateTime}});

             Onlinelistupdate = {"onlinelist":{ "userID": botUserNodes[j].userID, "userName": botUserNodes[j].userName,
             "UserGender": botUserNodes[j].UserGender, "Profile_Provider": botUserNodes[j].ProfilePro, "Profile_user_name": botUserNodes[j].Profilename, "Profile_user_picture": botUserNodes[j].profilepicture, "UserRegion": botUserNodes[j].nameregion,
             "channelID": botUserNodes[j].channel, "istor": botUserNodes[j].IsTor, "channelName": botUserNodes[j].channelName, "camonoff": botUserNodes[j].camonoff, "ABOUTME": botUserNodes[j].ABOUTME, 
              "lat": botUserNodes[j].lat, "lng": botUserNodes[j].lng, "namecity": botUserNodes[j].namecity, "nameregion":  botUserNodes[j].nameregion, "ismobile": botUserNodes[j].ismobile,  "namecountry":  botUserNodes[j].namecountry}};   

            sendBotMsg(ajaxChat.messagedata, ajaxChat.channelID,Onlinelistupdate);    
                  }
                  botsLoop();
                }
              }, 10000);
            }

2 个答案:

答案 0 :(得分:1)

这可能对你有所帮助:

&#13;
&#13;
var j = 0;

function botsLoop() {
  setTimeout(function () {
    if (j < 50) {    
      console.log('timeout function');
      var times = 5;
      for (var i=0; i < times; i++){
        console.log('inner loop: ' + (i + 1));
        j++;
      }
      botsLoop();
    }
  }, 10000);
}

botsLoop();
&#13;
&#13;
&#13;

我认为你可以自己将它应用到你的需求中。

答案 1 :(得分:1)

首次调用bots循环时将初始值传递给function botsLoop(j) { setTimeout(function () { for (var startloop = 0; startloop <= 5;startloop++) { var dateTime = new Date(); ajaxChat.messagedata = ({ "messageinfo": { "userID": botUserNodes[j].userID, "userName": botUserNodes[j].userName, "userRole": botUserNodes[j].userRole, "UserGender": botUserNodes[j].UserGender, "Profile_Provider": botUserNodes[j].ProfilePro, "Profile_user_name": botUserNodes[j].Profilename, "UserRegion": botUserNodes[j].nameregion, "channelID": botUserNodes[j].channel, "istor": botUserNodes[j].IsTor, "channelName": botUserNodes[j].channelName, "text": 'userenterinchat,' + botUserNodes[j].lat + ', ' + botUserNodes[j].lng + ', ' + botUserNodes[j].namecity + ', ' + botUserNodes[j].nameregion, "dateTime": dateTime } }); Onlinelistupdate = { "onlinelist": { "userID": botUserNodes[j].userID, "userName": botUserNodes[j].userName, "UserGender": botUserNodes[j].UserGender, "Profile_Provider": botUserNodes[j].ProfilePro, "Profile_user_name": botUserNodes[j].Profilename, "Profile_user_picture": botUserNodes[j].profilepicture, "UserRegion": botUserNodes[j].nameregion, "channelID": botUserNodes[j].channel, "istor": botUserNodes[j].IsTor, "channelName": botUserNodes[j].channelName, "camonoff": botUserNodes[j].camonoff, "ABOUTME": botUserNodes[j].ABOUTME, "lat": botUserNodes[j].lat, "lng": botUserNodes[j].lng, "namecity": botUserNodes[j].namecity, "nameregion": botUserNodes[j].nameregion, "ismobile": botUserNodes[j].ismobile, "namecountry": botUserNodes[j].namecountry } }; sendBotMsg(ajaxChat.messagedata, ajaxChat.channelID,Onlinelistupdate); j++; if (j < 50) { botsLoop(j); } } startloop = 0; }, 10000) } botsLoop(0); // when called first time ,保留该值:

\