简体   繁体   English

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

[英]Only run second iteration of a loop on demand

I have 2 loops, the first loop is a very short loop and run very fast and the second loop is a long loop and takes much more time. 我有2个循环,第一个循环是一个很短的循环,运行速度非常快,第二个循环是一个长循环,需要更多时间。

I am passing info from the first loop into the second loop, and when the second loop finishes it will call "finish", and it needs to then trigger the first loop to run its second iteration,it will pass the second iteration info into the second loop again, and when the second loop finishes again, it will call finish" then trigger the first loop to run the third iteration. 我正在将信息从第一个循环传递到第二个循环,当第二个循环完成时它将调用“完成”,然后需要触发第一个循环来运行其第二次迭代,它将第二次迭代信息传递到第二个循环,当第二个循环再次结束时,它将调用finish”,然后触发第一个循环以运行第三次迭代。

And the process continues until the first loop finishes all its iteration. 并且该过程一直持续到第一个循环完成其所有迭代为止。

How would i approach it? 我将如何处理? I have tried the below but it stops after the first iteration for the first loop. 我已经尝试了下面的方法,但是在第一个循环的第一次迭代之后它停止了。 I just need the loop to stop after each iteration and when ondemand(a trigger) it will go to the next iteration. 我只需要循环在每次迭代之后停止,并且在ondemand(触发器)时它将进入下一个迭代。

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

Or maybe run it in a different way but i am not sure if it is possible or not. 或者也许以不同的方式运行它,但我不确定是否可行。

basically I will have different users which i have to run in a loop, also loop through messages for each person. 基本上,我将有不同的用户,这些用户必须循环运行,也要遍历每个人的消息。 But i have to wait til the message loop is completed before iterate to the next person, because i have to set sessionstorage for the person's message, if it doesn't wait for the message loop to complete, then it won't save to the correct person. 但是我必须等到消息循环完成后再迭代到下一个人,因为我必须为该人的消息设置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]
    }
    }

Edit 编辑

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

Edit Strophe RSM plugin 编辑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');
        }

      }

    }

  }
};
}));

As i understand, your query call issues an async operation, and you can't get the result in the same call context which issues the request. 据我了解,您的query调用发出异步操作,并且您无法在发出请求的同一调用上下文中获得结果。

The onMessage event will fire only when your loop is ended, so you need to provide a means to handle the context in the callback itself. 仅在循环结束时才会触发onMessage事件,因此您需要提供一种在回调本身中处理上下文的方法。

Also if you issue all the asynchronous requests inside a loop you can't infer the order in which the operations finish. 同样,如果您在循环内发出所有异步请求,则无法推断操作完成的顺序。

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.

If you need a strict order, then you have to issue the requests one by one, but not in a loop, but the next request should be issued in the previous request's callback. 如果需要严格的命令,则必须一个接一个地发出请求,而不是循环发出,但下一个请求应在前一个请求的回调中发出。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM