1

I know that this kind of question has been asked over and over again (I have read and reread many on Stackoverflow and elsewhere). I try closures, anonymous functions and the rest, but I do not get anything because I'm not a guru code ):.

Having said that here's my problem:

function A (a) {
  for (var i in a) {
    var x = a[i];
    function B (x); // wait for the end of the function B before next loop
  }
}

....

function B (x) {

 //do things with "x" and repeat the function B until a condition is true and  then return to the function A
}

I tried many things (probably not enough) but none work properly ...

So thank you in advance for your suggestions

Regards

Edit :

Ok here the full code :

  var Test  = {

  ......

  setProgress: function(count) {
   var win = Test.getMainWindow();
   var progressMeter = win.document.getElementById("progressMeter");
   progressMeter.value =  count;
  }, 

  cancelCheck: function(event) {
    var win = Test.getMainWindow();
    win.document.getElementById("cancel").value = "cancel";
  },


  timeout: function (item) {

      var win = Test.getMainWindow();
      win.document.getElementById("progressMeterText").value = item;
      var stat = Test.getStatus()

       if (stat !== "loading" || win.document.getElementById("cancel").value == "cancel") {

      // if cancel button is clicked         
      if (win.document.getElementById("cancel").value == "cancel") {

        Test.setProgress(100);
        Test.stop();
        win.document.getElementById("cancel").value = "";
        win.document.getElementById("progressMeter").collapsed=true;
        win.document.getElementById("cancel").hidden=true;
        win.document.getElementById("progressMeterText").hidden=true;

      }
      // end of the loading
      else if (stat !== "loading") {
        Test.setProgress(100);
        win.document.getElementById("progressMeter").collapsed=true;
        win.document.getElementById("cancel").hidden=true;
        win.document.getElementById("progressMeterText").hidden=true;
        win.document.getElementById("cancel").value = "";
      }

      // loading is running
      else {        
        count = count+5;
        if (count == 100) {
          count = 0;
        }
        Test.setProgress(count);
        setTimeout (Test.timeout, 100 , item);
       }
     } 
   },

   getStuff: function (items) {
    for(var i in items) {
      var item = items[i];

        if (!item.host || (items && item.id != items)) continue;

        var listener = new Test.GetListener(item);
        listener.instance = new TestGet(item, listener).execute();

        count = 10;
        Test.setProgress(count);
        var buttoncancel = win.document.getElementById("cancel");
        buttoncancel.addEventListener ('click', Test.cancelCheck, false);

        Test.timeout(item);

        .....

    }
        .....
  }

}
6
  • 3
    In the first function (A), you mean just B(x) and not function B(x), right? If not, well, that may be your problem, but since you forgot to say what's going wrong I can't know for sure. Commented Sep 12, 2012 at 13:32
  • 1
    Functions won't return until they're complete, unless you do something asynchronous. Without more information, there's no way to help. (Even then, the function returns, but the asynch operation likely won't have completed yet.) Commented Sep 12, 2012 at 13:33
  • Does B initiate an asynchronous operation (e.g. setTimeout)? If so, you need to include that in your code. Commented Sep 12, 2012 at 13:34
  • 1
    ... it would really help if you post your complete last attempt, or jsfiddle of it. Commented Sep 12, 2012 at 13:35
  • @Pointy : you're right : it's B(x) ... the problem is that when "a" (which is an Array) contain just one item it's work but with more items no Commented Sep 12, 2012 at 13:48

2 Answers 2

2

It seems that B is a recursive function, but you should still call it just by B();.

function A(a) {
  for (var i in a) {
    B(a[i]); 
  }
}

function B(x) {
   var ret = dosomething(x);
   if (condition(ret)) {
     return;
   }
   return B(ret);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@BillyBoy1 Check your logic, it is because you don't met the condition to finish the recursion.
humm, i'm sorry but i don't understand. in your example, b(x) is timeout(item) in my code right ? So i need to create another function "dosomething(item)" which fire timeout(item) which contain the condition to finish the recursion ?
0

I'm no code guru either, but i do see one thing that may help.

since you have one var x it will continuously replace the var x so (i'm assuming you're using an array) you'll only have one value, the last item in the array, as a value for x. Therefore, your function B will run once with the value of the last item in your array from function A. If that's not what you're looking for, try creating a unique variable for each item in the array, or simply function B (a[i]); with an if/else or switch/case statement to only return the information you wish when appropriate.

i'm still getting a grasp on js, so i hope that was at least a little helpful. good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.