仅按需运行循环的第二次迭代

时间:2015-09-22 07:39:01

标签: javascript loops

我有2个循环,第一个循环是一个非常短的循环并且运行得非常快,第二个循环是一个长循环并且需要更多时间。

我将信息从第一个循环传递到第二个循环,当第二个循环结束时,它将调用"完成",然后需要触发第一个循环来运行第二个循环,它将第二次迭代信息再次传递到第二次循环,当第二次循环再次完成时,它将调用 完成"然后触发第一个循环以运行第三次迭代。

此过程一直持续到第一个循环完成所有迭代。

我将如何接近它?我已经尝试了下面但它在第一次循环的第一次迭代后停止。我只需要在每次迭代后停止循环,当ondemand(触发器)它将进入下一次迭代。

 for (var i=0; i<from.length; i++) {
 if (loopfinished=true){
  }}

或者也许以不同的方式运行它,但我不确定它是否可能。

基本上我会有不同的用户,我必须在循环中运行,也循环通过每个人的消息。但我必须等到消息循环完成后再迭代到下一个人,因为我必须为人的消息设置sessionstorage,如果它不等待消息循环完成,那么它不会挽救正确的人。

   var people=["user1@server","user2@server","user3@server"]

   // function to loop through messages for each person 
     for (var i=0; i<from.length; i++) {
    //load all the info here, when complete it will call done
    if(done){
    // when completed first person set people[2], when people[2] is done run people[3]
    }
    }

修改

       var messageno=0;  
       var loopfinished=true; 
    var from=["user1@server","user2@server","user3@server"]
     for (var i=0; i<from.length; i++) {
     if (loopfinished){
      console.log(from[i]); 

var person=from[i]
connection.mam.query(jid, {
  "with": person,"before": '',"max":"10",
    },onMessage: function(message) {
 var message ="<div id=log2><br>'"+msg_data.message+"'</div>"

     messageno= messageno+1;
      console.log( messageno);


       if(messageno==count){
                 loopfinished=true;
      console.log("Inner loop completed");
           console.log(loopfinished);
      }
    return true;
}}

编辑Strophe RSM插件

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define("strophe.rsm", [
            "strophe"
        ], function (Strophe) {
            factory(
                Strophe.Strophe,
                Strophe.$build,
                Strophe.$iq ,
                Strophe.$msg,
                Strophe.$pres
            );
            return Strophe;
        });
    } else {
        // Browser globals
        factory(
            root.Strophe,
            root.$build,
            root.$iq ,
            root.$msg,
            root.$pres
        );

    }
}(this, function (Strophe, $build, $iq, $msg, $pres) {

Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');

Strophe.RSM = function(options) {
  this.attribs = ['max', 'first', 'last', 'after', 'before', 'index', 'count'];


  if (typeof options.xml != 'undefined') {

    this.fromXMLElement(options.xml);
  } else {
    for (var ii = 0; ii < this.attribs.length; ii++) {
      var attrib = this.attribs[ii];
      this[attrib] = options[attrib];
       console.log("done5");   
    }

  }
};

Strophe.RSM.prototype = {
  toXML: function() {
    var xml = $build('set', {xmlns: Strophe.NS.RSM});
    for (var ii = 0; ii < this.attribs.length; ii++) {

      var attrib = this.attribs[ii];
      if (typeof this[attrib] != 'undefined') {
        xml = xml.c(attrib).t(this[attrib].toString()).up();
        console.log("done6");
      }

    }
    return xml.tree();

  },

  next: function(max) {
    var newSet = new Strophe.RSM({max: max, after: this.last});
    return newSet;

  },

  previous: function(max) {
    var newSet = new Strophe.RSM({max: max, before: this.first});
    return newSet;

  },

  fromXMLElement: function(xmlElement) {
    for (var ii = 0; ii < this.attribs.length; ii++) {

      var attrib = this.attribs[ii];
      var elem = xmlElement.getElementsByTagName(attrib)[0];
      if (typeof elem != 'undefined' && elem !== null) {

        this[attrib] = Strophe.getText(elem);
        if (attrib == 'first') {
            console.log("done6");  
          this.index = elem.getAttribute('index');
        }

      }

    }

  }
};
}));

1 个答案:

答案 0 :(得分:0)

据我所知,您的query来电会发出异步操作,而您无法在发出请求的同一个调用上下文中获得结果。

onMessage事件只有在你的循环结束时才会触发,所以你需要提供一种方法来处理回调本身的上下文。

此外,如果您在循环内发出所有异步请求,则无法推断操作完成的顺序。

var count = users.length;
for (var u in users)
{
 var user = users[u];
 query({onMessage:(function(user){return function(message){
   count --;
   processUserMessage(user, message); // some operation on a user and a message
   if (count === 0)
      allFinished();   
 };})(user)});
}
// At this point, none of the requests have returned. They will start arriving later.

如果您需要严格的订单,那么您必须逐个发出请求,但不能在循环中发出请求,但是下一个请求应该在之前请求的回调中发出。

var processChunk = function(user)
{
   query({onMessage:function(message){
     processUserMessage(user, message);
     var nextUser = findNextUser(user);
     if (nextUser)
         processChunk(nextUser);
     else
         allFinished();
   }});
};

processChunk(firstUser);
// At this point, none of the requests have returned. They will start arriving later.
相关问题