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);
.....
}
.....
}
}
B(x)and notfunction 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.Binitiate an asynchronous operation (e.g.setTimeout)? If so, you need to include that in your code.