在我的页面中,我有以下将要加载的脚本:
<script src="~/js/dir/search.js"></script>
<script src="~/js/dir/item.js"></script>
在search.js
内部文件中,我在全局范围内使用ES6:
fetch(endpoint)
.then(blob => blob.json())
.then(data => members.push(...data));
在item.js
内,我使用此脚本显示members
对象:
$(window).load(() => {
console.log(members);
});
但是它在控制台中打印undefined
,如何确保search.js
完全加载,然后items.js
给出结果?
答案 0 :(得分:1)
您可以从一个文件公开API以在另一个文件中注册一个额外的处理程序。
search.js:
var p = fetch(endpoint);
p.then(…);
window.extraHandler = function (fn){
p.then(fn);
}
item.js:
extraHandler(function(){
console.log(members);
});