wait() function is not getting called
karanave opened this issue · 2 comments
i have labscript something like
$LAB.script("https://.....wa_config.js").wait(function(){loadSCLibrary();});
function loadSCLibrary(){
}
suppose if wa_configs.js is not loaded (either 3rd party server is down or file not available), than loadSCLibrary() is not getting called.
i wanted to run loadSCLibrary() even if wa_configs.js is not loaded. and i should want loadSCLibrary() to run only after checking/loading wa_config.js file.
Thanks in advance.
Sai
Unfortunately, this is not really directly possible, cross-browser, at least by the LABjs library. The problem is, browsers are inconsistent about how they handle a "failed to load" script. Some fire an error, some never fire anything, some fire a load under certain network error conditions (older browsers I believe).
So, LABjs really cannot tell if it didn't load, only if it did successfully load.
The assumption with the normal use of LABjs is that you do not want other code to run if previous code fails to run, as this is usually a bad idea, and will just cause cascading errors.
Since you're in an exception case, you'll need to do something different, manually.
My suggestion is to use a timeout interval, perhaps like this:
(function(){
var flagged = false;
var timer = setTimeout(loadCSLibrary, 10000); // give it 10 secs max to load
$LAB
.script("https://.....wa_config.js")
.wait(function(){
loadSCLibrary();
});
function loadSCLibrary(){
clearTimeout(timer);
timer = null;
if (!flagged) {
flagged = true; // mark this code as having run already
// ... your code
}
}
})();
Hopefully that kind of helps illustrate how to construct a timeout for your scenario. :)
thanks for clarification... this will help....