cancel
Showing results for 
Search instead for 
Did you mean: 

Lines of code are not executed in the order they are written... Why?

0 Kudos

I have the following fragment of code:


<script>

  someGrid = new dhtmlXGridObject('someGridContainer');
  // additional someGrid settings...

  someGrid.loadXML("url", function () {
    console.log('inside loadXML');
  });
  console.log('after loadXML');

</script>

If I run it on my local PC, I will receive:

inside loadXML

after loadXML

But when executing this within MII, I will receive:

after loadXML

inside loadXML

Why?

Accepted Solutions (1)

Accepted Solutions (1)

former_member185280
Active Contributor
0 Kudos

It looks like your loadXML method is executing asynchronously, getting whatever is located at the url you are passing,  and then calling your anonymous function when complete. This is probably just happening faster on your local pc.

0 Kudos

I supposed it so... but I'm receiving the same order of messages for this case too:

<script>

  someGrid = new dhtmlXGridObject('someGridContainer');
  // additional someGrid settings...

  someGrid.loadXML("url", function () {
    console.log('inside loadXML');
  });
  for ( var i = 0 ; i < 1000000 ; i++) { i += 1; i -= 1;}
  for ( var i = 0 ; i < 1000000 ; i++) { i += 1; i -= 1;}
  for ( var i = 0 ; i < 1000000 ; i++) { i += 1; i -= 1;}
  for ( var i = 0 ; i < 1000000 ; i++) { i += 1; i -= 1;}
  for ( var i = 0 ; i < 1000000 ; i++) { i += 1; i -= 1;}
  console.log('after loadXML');

</script>

swaroop_anasane
Active Contributor
0 Kudos

Hi Tibor,

Looks like your object is calling a function like ajax call and yes as Christian mentioned asynchronously, so i only when it completes entire executtion, it leaves the page and goes for the URL. Check if loadxml() has a parameter to turn it into synchronous call.

Best Regards

Swaroop

0 Kudos

Hi Swaroop,

unfortunately "load()" functions are only asynchronous..

but I still don't understand this story... I had to wait at least 3 seconds until the five dummy for-loops finished their execution, and then both of the log-messages appeared immediately in the following order:

after loadXML

inside loadXML

Isn't there any setting in MII that makes this behavior?


former_member185280
Active Contributor
0 Kudos

This behavior has nothing to do with MII. This is all happening on the browser/client and is normal behavior. Even tho the call to go get the document from MII is async, Javascript is single threaded. The javascript engine asks the browser to go get the doc from the url and let it know when its done. The engine then continues doing whatever else it needs to do. When the browser is done getting the doc it lets the engine know. When the engine gets a chance and its not busy it will get to it and execute your callback function. So your browser may have gone and gotten the document but your javascript engine is still busy looping or whatever so your callback has to wait. If you want your 'after Load' to always execute after your 'inside load' you have to make your load call synchronous, or put it in your callback function, or use a promise.

swaroop_anasane
Active Contributor
0 Kudos

Hi Tibor,

Christian just answered your question. Nothing to do qith MII, you can use any solution suggested above.

Thanks,

Swaroop

0 Kudos

may I have a last question? consider the following code:

function f () {

   someGrid = new dhtmlXGridObject('someGridContainer');
   // additional someGrid settings...

   someGrid.loadXML("url", function () {
      console.log('inside loadXML');
   });
   console.log('after loadXML');

}


Function f() is called and loadXML() function is being executed inside it... May we leave function f() during the execution of loadXML() ?

swaroop_anasane
Active Contributor
0 Kudos

Hi Tibor,

It anyways continues execution of the code block even if the xml is not completely loaded. May I know what are you planning to achieve when u sau you want to leave this function?

Best Regards,

Swaroop

0 Kudos


I'm receiving the following error in my application:

Unable to get property '_childIndexes' of undefined or null reference

and I'm afraid, this is because loadXML() is being executed and we already left function f() and another code is executed, which expects that loadXML() was finished...

swaroop_anasane
Active Contributor
0 Kudos

Try below:

If you want to wait until all ajax requests are finished in your document, no matter how many of them exists, just use $.ajaxStop event this way:


  $(document).ajaxStop(function () { // 0 === $.active });


Reference: Wait until all jQuery Ajax requests are done? - Stack Overflow

Hope this helps.

Regards,

Swaroop

former_member185280
Active Contributor
0 Kudos

Javascript is event driven. Especially when you get into a lot of async/ajax stuff. That is why the api you are using provides the ability to specify a callback function. This does mean that often huge portions of your application logic will need to be orchestrated into functions.

0 Kudos

Yes or No?

If i'm in function f() -> loadXML()... and loadXML() is being executed... then could it happen, that we reach the end of function f() and we return back the execution to the place where f() was called? And in the meantime loadXML() is still running?

former_member185280
Active Contributor
0 Kudos

No, loadXML is not still running. Asnyc processes like AJAX calls that loadXML may have triggered may still be running but loadXML is done. I recommend taking a step back possibly looking at some JavaScript tutorials. Particularly around AJAX.

swaroop_anasane
Active Contributor
0 Kudos

It will come back to the function only to execute portion under success or callback of ajax. The focus would not come back to anywhere in the function outside ajax's success.

-Swaroop

Answers (1)

Answers (1)

0 Kudos

Hi Tibor,

I can suggest a workaround of applying some delay or time lag in order to make the function execute after certain time.

Let me know if this works.

-Snehal

0 Kudos

hi Snehal,

thanks, it also occured to me, but didn't try