这怎么评价为假呢?
当我单步执行此代码时,图中的值会导致 cssLoaded = 0;据我所见,长度大于 0 并且 cssStylesheet.sheet 具有属性,因此应该为 true,因此 cssLoaded 应设置为 1,但它没有发生。我一定在这里遗漏了一些东西..?
function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
console.log('oops');
}
if (cssStylesheet.href == stylePath){
try {
if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 ){
cssLoaded = 1;}
else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 ){
cssLoaded = 1;}
else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ){
cssLoaded = 1;}
}
catch(ex){ }
}
if(cssLoaded) {
//alert('cssloadedcomplete');
resetPops();
$('#video-overlay').show();
positionElements();
saveBizzmail();
console.log('try:' + tryCount)
} else {
tryCount+=1;
console.log('try:' + tryCount);
setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
}
}
When I step through this code, the values pictured result in cssLoaded = 0; From what I can see, the length is greater than 0 and cssStylesheet.sheet has properties so should be true, therefore cssLoaded should be set to 1, however it's not happening. I must be missing something here..?
function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
console.log('oops');
}
if (cssStylesheet.href == stylePath){
try {
if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 ){
cssLoaded = 1;}
else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 ){
cssLoaded = 1;}
else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ){
cssLoaded = 1;}
}
catch(ex){ }
}
if(cssLoaded) {
//alert('cssloadedcomplete');
resetPops();
$('#video-overlay').show();
positionElements();
saveBizzmail();
console.log('try:' + tryCount)
} else {
tryCount+=1;
console.log('try:' + tryCount);
setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码可以通过三种方式获得 cssLoaded == 0;
if (cssStylesheet.href == stylePath)
计算结果为false
。if/else
语句的计算结果均为false
。if/else
语句引发异常,因此其他if/else
语句不会执行。在解决问题时,您应该验证前两个不是问题,因为这些可能只是比较中的逻辑错误,或者进入函数的数据不是您所期望的。
对于第三个问题,您可以实现防御性编码,以确保通过将代码更改为此来检查所有三个
if/else
语句(您甚至不再需要 try/catch,因为它允许您的代码绕过一些测试):There are three possible ways your code gets cssLoaded == 0;
if (cssStylesheet.href == stylePath)
evaluates tofalse
.if/else
statements evaluate tofalse
.if/else
statements throws an exception and thus the otherif/else
statements aren't executed.In troubleshooting your issue, you should verify that the first two are not the issue as those are probably just logic errors in the comparisons or the data coming into the function isn't what you expected it to be.
For the third issue, you can implement defensive coding to make sure that all three
if/else
statements are checked by changing your code to this (you don't even need the try/catch any more because it allows your code to bypass some of the tests):前一个检查可确保您避免类似 NPE 的情况(通过检查
cssStylesheet.sheet
不为 null)。The former check ensures that you avoid an NPE like situation (by checking that
cssStylesheet.sheet
is not null).您必须检查该元素是否存在,否则可能会发生错误(
cssStylesheet.sheet
isundefined
)。在以下情况下,表达式的计算结果为 false:undefined
、null
、void 0
)0
code>(零)false
""
、''
)0/0,
NaN
)请注意,空对象
{}
的计算结果为 true,因为它是一个对象。另一个例子:
如果不存在 if 条件,则代码将在旧版本的 IE 中抛出错误,因为旧版本的 IE 没有名为
window.addEventListener
的方法。You have to check for the existence of the element, otherwise an error may occur (
cssStylesheet.sheet
isundefined
). An expression will evaluate to false when:undefined
,null
,void 0
)0
(zero)false
""
,''
)0/0
,NaN
)Note that an empty object
{}
evaluates to true, because it's an object.Another example:
If the if-conditions were not present, the code will throw an error at older versions of IE, which don't have such a method called
window.addEventListener
.