kubetail-org / loadjs

A tiny async loader / dependency manager for modern browsers (899 bytes)
MIT License
2.58k stars 150 forks source link

Ready is triggered before the library is loaded #73

Open nachitox opened 6 years ago

nachitox commented 6 years ago

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

amorey commented 6 years ago

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?

nachitox commented 6 years ago

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');
});
amorey commented 6 years ago

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) {}
});
saurabhnanda commented 4 years ago

I'm seeing similar behaviour.

amorey commented 4 years ago

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

DoubleCorner commented 3 years ago

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 commented 3 years ago

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 ???

amorey commented 3 years ago

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?

DoubleCorner commented 3 years ago

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'

amorey commented 3 years ago

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 commented 6 months ago

@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?

DoubleCorner commented 6 months ago

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