loadjs icon indicating copy to clipboard operation
loadjs copied to clipboard

Ready is triggered before the library is loaded

Open nachitox opened this issue 7 years ago • 12 comments

daterangepicker.js is loaded after moment, which loads after jquery.

load

Sometimes it works fine, but other times daterangepicker does not exists.

error

I'd prefer waiting a few ms than the library throwing an error.

What can I do? Thank you

nachitox avatar Aug 02 '18 16:08 nachitox

It looks like the screenshot got cut off so I can't see how jQuery is being loaded. Can you share the code that is causing the error?

amorey avatar Aug 02 '18 19:08 amorey

jquery and moment are loaded using loadjs too

loadjs('https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', 'jquery');
loadjs.ready('jquery', function() {
loadjs([REAL_PATH + 'moment/moment.min.js',], 'moment');
});

nachitox avatar Aug 03 '18 12:08 nachitox

I don't know the internals of daterangepicker.js but it's possible that it has a dependency on DOMContentLoaded that is causing the intermittent failure. Have you tried wrapping your code with jQuery.ready()?

loadjs.ready('datepicker', function() {
  $.ready(function() {
    $('#datefrom').daterangepicker();
  });
});

In addition, your code appears to be loading jQuery, moment.js and daterangepicker.js sequentially. If you want to load them in parallel and execute them in order you can use async: false:

loadjs(['/path/to/jquery.js', '/path/to/moment.js', '/path/to/daterangepicker.js'], {
  async: false,
  success: function() {},
  error: function (pathsNotFound) {}
});

amorey avatar Aug 04 '18 00:08 amorey

I'm seeing similar behaviour.

saurabhnanda avatar Jul 20 '20 12:07 saurabhnanda

Did you try the suggestions above? https://github.com/muicss/loadjs/issues/73#issuecomment-410404667

amorey avatar Jul 20 '20 21:07 amorey

I'm seeing similar behaviour also. Miraculously, there is no problem in the my local PC, but there are occasional errors on the line

DoubleCorner avatar Mar 26 '21 08:03 DoubleCorner

addtionally, I have set the async is false I think the problem maybe is here.As long as async exists, whether it is true or false, it means asynchronous image

image if(async || async === undefined) e.async = true ???

DoubleCorner avatar Mar 26 '21 09:03 DoubleCorner

The javascript object accepts false as a value so if you set the async property to false then the scripts will not be loaded asynchronously (https://www.w3schools.com/jsref/prop_script_async.asp).

Can you share the problematic code so I can take a closer look at it?

amorey avatar Mar 27 '21 09:03 amorey

like it

if(hasLoad) {
    loadjs(['/path/to/map.js'], 'map' {
      async: false
    });
    hasLoad = true;
}
loadjs.ready(['map'],()=>{
 map.refresh(); // sometime map is undefined
})

In particular, it will be called twice during initialization, first is in 'componentDidMount', second is in 'componentWillReceiveProps'

DoubleCorner avatar Mar 29 '21 02:03 DoubleCorner

Without knowing what map.js does internally I can't tell what the problem is. There could be a dependency on DOMContentLoaded that is causing an intermittent failure as described above. Can you provide a working code snippet that I can run? If you can provide a working HTML file that demonstrates the problem that would make it easier to debug.

amorey avatar Mar 29 '21 08:03 amorey

@DoubleCorner I got an email notification that you left a new message but now I can't see the message. Did you figure out the problem?

amorey avatar Mar 15 '24 11:03 amorey

@amorey no, I will try again. if I figure out, I will write my solution. Thanks for your reply.

DoubleCorner avatar Mar 15 '24 11:03 DoubleCorner