Comments (7)
Great! I'm happy to hear you like loadjs!
Loadjs is meant to be used only as an async loader so it assumes that the order of the scripts doesn't matter. Internally it iterates through the scripts backwards since that's the fastest, most space efficient way to write a loop:
https://github.com/muicss/loadjs/blob/master/src/loadjs.js#L126
It sounds like you're looking for parallel loading with sequential execution. Can you use <script>
tags in the <head>
to implement it?
from loadjs.
Hi,
Yes I would like to use parallel loading with sequential execution so the scripts will not fail. :)
I have loadJS initiated here
<script src="/vendors/loadjs.min.js"></script>
</head>
<body>
and then all my scripts loaded before the
</body>
like this for example:
loadjs(['//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js', '//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js'], 'thunk');
and then some extra to load bits and pieces once all the scripts above are ready:
loadjs.ready(['thunk', 'frmfunc'],
function() {
// foo.js & bar.js & thunkor.js & thunky.js loaded
$('a').tooltip({trigger : 'hover',container: 'body'})
},
function(depsNotFound) {
if (depsNotFound.indexOf('thunk') > -1) {}; // foo failed
// if (depsNotFound.indexOf('boostr') > -1) {}; // bar failed
if (depsNotFound.indexOf('frmfunc') > -1) {}; // thunk failed
});
What should I do to have it working in parallel but respect the sequence?
Thank-you so much again!
from loadjs.
This simplest way to fetch files in parallel and execute them in sequence is to use <script>
tags in the HTML. For example this will fetch script1.js
and script2.js
in parallel and execute them in series:
<html>
<head>
<script src="/path/to/script1.js"></script>
<script src="/path/to/script2.js"></script>
</head>
<body>
</body>
</html>
Async loading libraries like loadjs
are useful for fetching files after the initial DOM has been rendered to improve performance. For example, you could use loadjs
to load Google Analytics after your page has loaded:
<html>
<head>
<script src="/path/to/script1.js"></script>
<script src="/path/to/script2.js"></script>
<script src="/path/to/loadjs.js"></script>
<script>
window.addEventListener('load', function() {
loadjs(['/path/to/google-analytics.js']);
});
</script>
</head>
<body>
</body>
</html>
from loadjs.
The thing is, wouldn't be faster if all is handled by loadjs to take care of all the scripts? also becuase for example you cant really add some scripts before and then some other modules to be loaded with loadjs, because they will be pushed before and therefore certain scripts will still fail... That's why I think loading in parallel but with sequential order should be respected in the loader so you can load all your scripts asych and have consistent result.
from loadjs.
In an ideal world it would be great to use an async loader (e.g. loadjs
) to fetch scripts in parallel and execute them in series. Unfortunately there many edge cases and currently it's beyond the scope of this particular library. I'd recommend reading this article for a deep dive into script loading:
http://www.html5rocks.com/en/tutorials/speed/script-loading/
Given all the edge cases, the simplest thing to do is to use <script>
tags in the <head>
when you want to fetch files in parallel and execute them in series, and loadjs
to load scripts asynchronously.
What are you trying to do in particular? There are a lot of things you can do with loadjs.done()
and loadjs.ready()
to keep your script loading fast and simple.
from loadjs.
@neuropass I added support for fetching files in parallel and executing them in series via an async: false
option:
loadjs(['/path/to/file1.js', '/path/to/file2.js'], {
success: function() { /* files loaded successfully */ },
async: false
});
The code is in the loadjs:asyncfalse
branch:
https://github.com/muicss/loadjs/tree/asyncfalse
Please try this file and let me know if it works for you:
https://raw.githubusercontent.com/muicss/loadjs/asyncfalse/dist/loadjs.min.js
from loadjs.
Support for fetching files in parallel and executing them in series is implemented in v2.0.0:
https://github.com/muicss/loadjs
Let me know if you run into any issues using the feature.
from loadjs.
Related Issues (20)
- How to Solve the Problem of Repeated Loading of Scripts HOT 3
- Is it possible to load JSON? HOT 2
- Stylesheets not loading in ie11 HOT 4
- Using loadjs in a Vanilla project: require is not defined error HOT 2
- CSS order HOT 2
- Security policy questions HOT 3
- Old browsers (Opera12) can't use document.head HOT 3
- Success/Error callbacks never called when the script element has the "nomodule" attribute. HOT 15
- More details than "path not found" HOT 2
- Announcement: New version with support for Promises and an Edge CSS onerror bugfix HOT 1
- Update comments in src to clarify arguments to loadJs HOT 1
- Custom script attributes HOT 3
- Ability to insert elements after or before certain element HOT 2
- Image loading and error handling HOT 2
- Support "defer" as well as "async"? HOT 6
- query args cause issues with native file type detection HOT 5
- Idempotent request option HOT 5
- Can not detect image if url has ?=rand123456 HOT 1
- Announcement: New version with support for image/css urls with query arguments and anchor tags
- Action required: Greenkeeper could not be activated 🚨
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loadjs.