1
如何在已有函數上添加函數?獲取「RangeError:超出最大調用堆棧大小」錯誤
我使用下面的方法,它給了我錯誤的resources.onload
resource = document.getElementById(this.id);
if (resource && resource.getAttribute('data-loading'))
{
onloadOthers = resource.onload;
onloadThis = this.onComplete.bind(this);
//following line give error
resource.onload = function() { // callback loops again and again!!!!!!
if (typeof onloadOthers == "function")
onloadOthers();
onloadThis();
};
return; // just added another callback, no need to add it again.
}
else if (resource)
{
this.onComplete();
return; // already exist
}
if (this.type == "js")
{ //if filename is a external JavaScript file
var code = document.createElement('script');
code.setAttribute("type", "text/javascript");
code.setAttribute('data-loading', 'yes');
code.setAttribute("src", this.file);
code.onload = this.onComplete.bind(this);
code.onreadystatechange = code.onload; // ie fix
}
該錯誤通常意味着一個永無止境的循環 – HMR
是的,我知道這一點。我該如何解決它?我想在新的回調之上添加以前的回調。所以他們都被觸發了。 – Basit
'onloadOthers'可能會導致無限遞歸。確保'unloadOthers'變量包含在新的閉包中。 –