-1

I have a jsp file with html content, and a script inside and I am also using dhtmlx. The other parts of my Web Application are working, so with this, I want only to focus on this problem because the environment works well. In one point of the file I have got:

numTabs = getNumTabs();
for (var i=0; i<numTabs;i++) {
    var mytab = "tab_"+i;
    tabbar.addTab(mytab,'Tab Numer: ' + i,'');
    //alert("for " + i); PLACE 1
    initTabContent(mytab);
}

function initTabContent(tabID){
    //alert("initTabContent " + i); PLACE 2
    tab = tabbar.cells(tabID);
    toolbar = tab.attachToolbar();
    toolbar.loadXML('.../...file.xml',function(){
           //alert(i); PLACE 3
           toolbar.setItemText('botton1', 'Botton 1');

    });
    grid = tab.attachGrid();
    //more stuff
}

The point is:

If I uncomment the alert in PLACE 1, everything goes pretty well, it loads the XML and everything is working but if I comment the alert it doesn't go well.

If I uncomment alert 1 and alert 2, I will see in the web page:

  1. I see: "for 0" -> OK
  2. I see: "for 1" that changes almost immediately to "initTabContent 0" -> OK but I can see it
  3. I see: "for 1" -> OK
  4. I see: "for 2" that changes almost immediately to "initTabContent 1" -> OK
  5. I see: "for 2" -> OK
  6. I see: "for 3" that changes almost immediately to "initTabContent 2" -> OK ....and on

So, I understand that despite of myfunction the loop goes on and doesn't wait the complete termination of that function. The funny part is that if I comment alert 1 and 2, and uncomment alert 3, the first thing I see on the web page is 4, then 3, 2, 1, 0...

And if I leave again, only alert 1 uncommented, everything goes ok. The reason about that I suppose is that the alert makes the system wait to the user and therefore the XML is well loaded.

My questions: First: Why the loop is not waiting for the function to finish? It should be sequential right? Second: How can I solve it without the alert? I also tried to put an empty for loop inside to make time but it didn't work out...(and that is not a good way to do it)

Thank you very much, Alex.

12
  • 2
    It is not sequential - it's asynchronous. The loadXML method makes an AJAX request (I'm guessing), and executes the provided function when it completes (whenever that is).
    – Ian
    May 13, 2014 at 18:13
  • You forgot the var on a few specific variables that need them.
    – Kevin B
    May 16, 2014 at 15:54
  • the vars that are missing are global as I said before, this is only a fragment.
    – Alek
    May 16, 2014 at 15:55
  • The fact that they are missing is the whole problem. toolbar = tab.attachToolbar(); will be overridden before the xml is loaded.
    – Kevin B
    May 16, 2014 at 15:55
  • @KevinB You are completely right. That var was missing. The reason I didn't check that was because it was working with the alert inside the loop, so how can that be possible? If the var wasn't defined, how can it work stopping the loop with an alert?
    – Alek
    May 16, 2014 at 16:10

2 Answers 2

0

Part of what is causing you trouble is that an 'alert' blocks execution, while the loadXML call is asynchronous. To get a more accurate depiction of what is happening in what order, use console.log instead of alert.

Are you trying to use the value of i in your call myFunction for some purpose other than alerts ?

If that is the case. Try replacing:

myfunction(i);

With:

(function (num) { return function() {myfunction(num)} })(i);
5
  • Depite of loadXML call is asynchronous, I still don't understand why that call goes backwards. I use console log and there are no errors. The error is then the for loop doesn't wait myfunction to finish and calls it again. And I need the value of the i because the code is a little bit more complicated that the example I gave. Could you re-write the replacing? Because I don't get it. Thank you very much. @BrandoTheBrave
    – Alek
    May 15, 2014 at 8:55
  • @Alek I have a few questions before I can update my answer: What kind of object is toolbar? Is it an Xml document? What browser are you testing this in? Are you using any XML libraries? May 15, 2014 at 14:32
  • It is a DHTMLXtoolbar, I am using those libraries. The toolbar is loaded from a XML file because it has more buttons and so... In myfunction I give them names and visibility properties. I think that the solution of the problem is to force the loop to wait. I tried a while loop and everytime myfunctions returned true, I had an i++ for the while loop to continue. But the result was the same, it didn't work out...
    – Alek
    May 15, 2014 at 17:45
  • I don't think you will have a whole lot of luck forcing the loop to wait. The way DHTMLXtoolbar.loadXML is designed prevents that. What you could do is use the onLoadFunction to call a function that keeps track of the number of xmls loaded, and when all of them have been loaded, you could loop through the toolbar objects. Your example code doesn't represent the problem you are having. If you could modify your example, I could make a more specific suggestion. May 15, 2014 at 18:03
  • Basically, depending on numTabs I had to create so many tabs. In each tab I need to put a toolbar with buttons and after that, I need to put a grid under it. The grid appears, and the toolbar also but, as the for loop is quite fast compared to the loadXML function, there is functionality that doesn't work properly, e.g. the name of the buttons. Putting the alert inside the loop, stops the loop waiting for the user and the loadXML function has enough time to do everything as it has to be.
    – Alek
    May 16, 2014 at 15:53
0

Thanks to @KevinB I realized that indeed the toolbar declaration was missing in the whole jsp file. The toolbar = tab.attachToolbar(); is overriden before the loading of the XML.

Now with **var** toolbar = tab.attachToolbar(); the problem is solved.

To see the reason that the loop doesn't behave properly, you can read the comments under the question.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.