简体   繁体   English

Webpacks build.js 的 OnLoad?

[英]OnLoad for webpacks build.js?

I receive a build.js generated with webpack from other department.我从其他部门收到了一个用 webpack 生成的 build.js。 I include it in the HTML file with我将它包含在 HTML 文件中

<script type="module" src="build.js"></script>

But there are a number of asynchronous requests and variables in there that I need to access.但是那里有许多我需要访问的异步请求和变量。 I cannot access them until the build.js is completely loaded.在 build.js 完全加载之前,我无法访问它们。 How can I check when it has been loaded?如何检查它何时加载?

If build.js is your code and you are comfortable in editing it, then you could create a Promise which would automatically be fulfilled when the asynchronous tasks are completed.如果 build.js 是您的代码并且您可以轻松编辑它,那么您可以创建一个Promise ,它会在异步任务完成时自动完成。 You can also use multiple promises and call all to ensure that all your variables are in place.您还可以使用多个 Promise 并调用all以确保所有变量都到位。

Example for Promise.all , taken from the documentation: Promise.all的示例,取自文档:

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
});
// expected output: Array [3, 42, "foo"]

If build.js is not something you are comfortable modifying, then you could read it or its documentation and find out whether there is an even hook for the event when its loading is completed.如果 build.js 不是您愿意修改的东西,那么您可以阅读它或其文档,并在加载完成时找出事件是否有一个偶数钩子。

If all else fails, then you can use setInterval to repeatedly check for all the resources that you need and once they are found, perform the operations you need along with clearInterval to stop the repeated check for them.如果一切都失败了,那么您可以使用setInterval重复检查您需要的所有资源,一旦找到,执行您需要的操作以及clearInterval以停止对它们的重复检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM