在加载脚本之前执行的Modernizr.load回调 [英] Modernizr.load callback executing before loaded script

查看:104
本文介绍了在加载脚本之前执行的Modernizr.load回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的javascript上的某个时刻,我有以下内容(使用Modernizr 2.6.2):

At a certain point on my javascript I have the following (using Modernizr 2.6.2):

Modernizr.load([{
    load: '/path/file.js',
    complete: function () {
        //do stuff
    }
}]);

除IE8以外,大多数情况下它运行良好,在IE8中大约有1次,它先执行回调,然后再加载脚本.我在回调函数和另一个file.js内部添加了一个断点.这两个脚本都在执行,只是有时会交换顺序.

It works great most of the time, except on IE8, about 1 time out of 3 it executes the callback first then the script being loaded. I am adding a break point on the callback and another inside file.js. Both scripts are being executed, just swapping the order at times.

我试图隔离并重现该错误,但是我做不到.每当我在页面上只有此代码时,它都可以正常工作(使用完全相同的Modernizr文件).因此,我相信我项目中的其他内容可能会造成干扰,但是我不知道接下来的10000行(真的!)的js代码是什么.有没有人经历过类似的事情或对导致这种行为的原因有任何想法?

I tried to isolate and reproduce the error, but I couldn't. It works fine every time when I have only this code on the page (using the exact same Modernizr file). For that reason I believe something else on my project could be interfering, but I have no clue on what to look next along the 10000 (really!) lines of js. Has anyone experienced anything similar or has any idea on what could cause this sort of behaviour?

我没有针对此特定情况寻找解决方法.这是我真的想保持原样的众多实例之一.

编辑1-新信息: 使用网络面板,我可以看到正在完成两个请求(这应该是),但是第一个请求始终返回空白.第二个请求显示正确的内容.当两个请求都完成后执行回调时,这很好用,但是有时回调在第一个(空)请求和第二个请求之间执行.那是它崩溃的时候!

EDIT 1 - NEW INFO: Using the network panel, I can see that two requests are being done (which is expected), but the first request always returns blank. The second request brings up the correct content. That works fine when the callback is executed after both requests are complete, but sometimes the callback is executed between the first (empty) and second request. That is when it crashes!

编辑2-新信息:好的,现在我可以复制它了.要100%确定我的假设,我将不得不深入了解javascript引擎,而我不是,所以我可能会犯错,但这就是我发现的东西:

EDIT 2 - NEW INFO: Ok seems like I can reproduce it now. To be 100% sure on my assumptions I would have to know the javascript engine in deep, which I don't, so I could be dead wrong, but here is what I found:

请求file1.js和file2.js.如果引擎忙于执行file1的回调时file2完成加载,则file2的执行将排在队列末尾的某个位置,这意味着在file2的回调之后.

Request file1.js and file2.js. If file2 finishes loading while the engine is busy executing file1's callback, file2 execution goes somewhere at the end of the queue, which means after file2's callback.

推荐答案

在对该主题进行了一些研究之后,我认为这个问题可能比起初看起来更普遍,但由于它与上下文有关而被忽略了,并且有不同的描述方式:

After doing some research on the subject, I think this issue may be more common than it seems at first, but has been overlooked because it is context dependent, and has different ways of being described:

Modernizr加载[A,B]似乎在A之前执行了B,这怎么可能?

YepNopeJS:回调在之前触发微小的JS文件已完全加载

https://github.com/SlexAxton/yepnope.js/issues/195

阅读此不错的文章(在yepnope页面上引用),关于IE上遇到的不一致之处,我想出了一个修复程序,该修复程序已通过github发送给yepnope维护者:

After reading this nice article (referenced on yepnope page), about the inconsistencies experienced on IE, I came up with a fix, which has been sent to yepnope maintainers via github:

http://github.com/SlexAxton/yepnope.js/pull/196

我不知道它是否会被接受,但是它解决了我的问题.

I don't know whether it is going to be accepted or not, but it solved my problem.

由于编辑库可能并不总是一个好主意,因此我建议采取以下解决方法:

As editing a library may not always be a good idea, I would suggest the following workaround:

//instead of
complete:function(){
    doStuff()
}
//try
complete:function(){
    setTimeout(doStuff, 0);
}

这篇关于在加载脚本之前执行的Modernizr.load回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆