Why is there a time delay for the document.write script tag? And why is the execution of the script tag delayed?

In my code, I created 5 iframes with a script tag in them to get responses from the server. we need to do this in parallel. In addition, due to problems between the domains, we did not choose Ajax technology, simply creating an iframe at the same time to create asynchronous requests.

<script type="text/javascript" href="http://www.example1.com/json.js"></script> <!-- //---------------------------------------------------------------------(1)--> <script type="text/javascript"> var url = "http://www.example2.com/getResponse/"; var count = 5; var callback = "callback"; function iframeCallback(index) { var iframe = document.createElement('iframe'); iframe.style.border='0px'; iframe.style.width ='0px'; iframe.style.height='0px'; document.body.appendChild(iframe); var content = "<script type='text/javascript'>"; content += "var begin = new Date();"; //------------------(2) content += "var jsText = \"<script type='text/javascript' src='" + url + "'></\" + \"script>\";"; content += "document.write(jsText);"; content += "</"+"script>"; content += "<script type='text/javascript'>"; content += "var data = eval('"+callback+"');"; //------------------(3) content += "window.parent.getRepsonse(data);"; content += "</"+"script>"; } function getRepsonse(data) { //Deal with the responses here //------------------------------------------------------------------(4) } function doMainProcess() { for (i=0; i<count; i++) { iframeCallback(i); } //pause the main thread here to wait until calls are finished //------------------------------------------------------------------(5) //go on to do something else } </script> 

My questions are here:

  • Why is there a time delay for the document.write script tag?

    When I debug the code above, I found that there is a time interval between (1) and (2). Is there a way to do these two cases at the same time? Or reduce the delay as short as possible.

  • Why is script tag execution delayed?

    The time delay between (2) and (3) is also strange. When we try to go directly to the url for the call, it only takes 150 ms, but if we use the script tag to make the call, it takes 400 ms +. You need the call time as close to the direct call as possible.

  • How can we pause the rest of the page loading until we get responses from iframes?

For some reason, we cannot just use the "setTimeout" function to create a time delay in (5).

I tried setting the flag to (4) when the last call answered, and then use the while-loop in (5) to pause the page loading there. but it seems to have no effect. If so, calls to the iframe will also be blocked until the while loop completes.

Is there a good way to pause the main thread in (5) in order to wait for all calls to complete?

+4
source share
1 answer
  • You cannot control when the browser displays DOMElements or loads resources; you can only respond to them.

  • If there is a delay, why not write a synchronization function that waits for the completion of the asynchronous process (for example, data collection)?

  • You cannot pause it, you just do not need to display or display it, but hide it.

What bothers me the most is what you think about creating a high-traffic service using technology that you don't seem to understand. You are trying to synchronize in a language that does not support synchronization.

Also, there is an error in your script, the content is not actually written.

And eval is evil. There must be a better way.

0
source

All Articles