这个弹窗怎么去除哇??删除什么代码可以去除~删除什么代码.可以去除~删除什么代码可.以去除~
You might currently have some code similar to this; 您当前可能有一些与此类似的代码;
function getWidthOfImage(src) {
var outerScopeVar;
var img = document.createElement('img');
img.onload = function() {
outerScopeVar = this.width;
};
img.src = src;
return outerScopeVar;
}
var width = getWidthOfImage('lolcat.png');
alert(width);
However, we now know that the return outerScopeVar
happens immediately; 但是,我们现在知道return outerScopeVar
立即发生。 before the onload
callback function has updated the variable. 在onload
回调函数更新变量之前。 This leads to getWidthOfImage()
returning undefined
, and undefined
being alerted. 这将导致getWidthOfImage()
返回undefined
,并且将向undefined
发出警报。
To fix this, we need to allow the function calling getWidthOfImage()
to register a callback, then move the alert'ing of the width to be within that callback; 为了解决这个问题,我们需要允许调用getWidthOfImage()
的函数注册一个回调,然后将宽度的警报移到该回调内;
function getWidthOfImage(src, cb) {
var img = document.createElement('img');
img.onload = function() {
cb(this.width);
};
img.src = src;
}
getWidthOfImage('lolcat.png', function (width) {
alert(width);
});
... as before, note that we've been able to remove the global variables (in this case width
). ...与以前一样,请注意,我们已经能够删除全局变量(在本例中为width
)。