How long does jquery.ready () function?

I have a page where php displays a fairly large div that has a lot of components inside it. JS to process this div starts almost instantly, but there are time issues associated with loading bigDiv and a script that process it.

So, I applied such code to process it on ready ()

var bigDiv = $(document).find('#bigDiv'); bigDiv.ready(function{ //some code here }); 

Now my question is: if bigDiv takes a long time, how long does the ready() function work? What if bigDiv is not displayed at all?

So, to avoid waiting for the script, I applied a timeout,

So I chose a different approach similar to this:

 var waitTimer = 0; var lodTmout = 2000; function chkDataLod(){ var bigDiv = $pH(document).find('#bigDiv'); if(bigDiv.length > 0){//find if bigDiv has content bigDiv.ready(function(){//wait for bigDiv to load it contents completely //some code here }); }else{ //chk for bigDiv after every 500 msec for 2000 msec if(waitTimer === lodTmout){ return false; }else{ waitTimer += 500; setTimeout('chkDataLod();',500); } } }; function _init(){ chkDataLod(); } _init(); 

Initially, I tried to see if bigDiv content was longer than zero. The problem arose when the "incomplete download" of bigDiv was displayed. So I added the .ready() function again. I have some animation features on this big Div that I don't want to run until it is fully loaded.

This bigDiv loaded from the CDN, so the loading time changes. Once it took almost 5 seconds to load. Seeing my own approach, I know that he has problems. I am using setTimeout and ready, both. But I'm not sure I'm ready to wait. I tried to see its documentation, but could not find it clearly.

How can I solve this problem for this situation?

+4
source share
3 answers

The easiest way is to run the code immediately after the closing tag of your element. Like this

 <div id='bigDiv>....</div> <script> // do whatever you want to do with div </script> 

ps: or you can try to access any element after the div at some interval. if it exists - your div is ready.

+1
source

It depends on how fast the client Internet is, how big the content in bigDiv is, and how fast the client computer is.

0
source

try adding a script tag to the first line

 var d = new Date(); console.log(d.getTime()); 

add the same after jquery ready function and see

compair this time with page load time

and you will notice that the ASA loads the last image that starts in the next millisecond

0
source

Source: https://habr.com/ru/post/1392418/


All Articles