我可以同步多个图像加载调用吗?
我希望在加载特定图像时运行一个函数,但我不知道如何在运行之前等待两者加载。我只知道如何链接它们,如下所示:
Image1 = new Image();
Image1.src = 'image1-link.jpg';
Image2 = new Image();
Image2.src = 'image2-link.jpg';
Image1.onload = function() {
Image2.onload = function() { ... }
}
这样做的缺点是它必须等到 Image1 完全加载才能获取第二个。我想尝试这样的事情:
Image1 = new Image();
Image1.src = 'image1-link.jpg';
Image2 = new Image();
Image2.src = 'image2-link.jpg';
(Image1 && Image2).onload = function() { ... }
编辑:谢谢你们 Paul Grime 和 ziesemer 的帮助。你们两个的回答都很好。我想我必须给Paul Grime最好的答案,虽然他使用了更多的代码,但我只需要知道图像的src并且内置了onload函数,而在ziesemer的答案中我需要知道src和计数图像,并且必须编写多个 onload 行。
根据具体情况,两者都有其目的。谢谢你们的帮助。
I want a function to run when specific images are loaded, but I don't know how to wait for both to load before running. I only know how to chain them, like below:
Image1 = new Image();
Image1.src = 'image1-link.jpg';
Image2 = new Image();
Image2.src = 'image2-link.jpg';
Image1.onload = function() {
Image2.onload = function() { ... }
}
The downside to this is it has to wait till Image1 completely loads before getting the second. I want to try something like this:
Image1 = new Image();
Image1.src = 'image1-link.jpg';
Image2 = new Image();
Image2.src = 'image2-link.jpg';
(Image1 && Image2).onload = function() { ... }
EDIT: Thank you both for the help Paul Grime and ziesemer. Both your answers are very good. I think I have to give the best answer to Paul Grime, although his uses more code, I only need to know the src of the images and the onload function is built in while in ziesemer's answer I need to know both the src, the count of the images, and have to write multiple onload lines.
Depending on the situation, both have their purpose. Thank you both for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个小提琴可能会有所帮助。这是一个简单的通用“加载器”。
http://jsfiddle.net/8baGb/1/
代码:
This fiddle might help. It's a simple generic 'loader'.
http://jsfiddle.net/8baGb/1/
And the code:
如果您无法使用 jQuery 之类的功能,请创建一个辅助函数,您可以将其传递给您感兴趣的所有图像的
onload
。每次调用它时,都会增加一个计数器,或者添加集合的src
名称。一旦计数器或您的集合达到您期望的所有图像的大小,您就可以开始执行您真正想做的任何工作。像这样的东西:
If you can't use something like jQuery, create a helper function that you can pass to the
onload
of all your images you are interested in. Each time it is called, increment a counter, or add thesrc
name to a set. Once the counter or your set reaches the size of all of the images that you are expecting, then you can fire off whatever work you really wanted to do.Something like:
我采用了上面 Paul Grime 的例子,并将其修改为更容易理解,同时仍然满足 OP 需求。我觉得这里的其他答案要么太复杂,要么不够。希望这段代码更容易理解和输入。我在每个循环中使用了下划线,但这很容易改变。
另外,我现在改进了解决方案,对 img 错误进行回调。如果加载了图像,则会将其添加到数组中,如果加载失败,则不会将其添加到数组中。我也将其更改为使用 jquery。
I took Paul Grime's example above and modified it to be more understandable while still accomplishing the OP need. I felt the other answers here were either to complicated or not enough. Hopefully this code is a little easier to pick up and put in. I used underscore for the each loop, but this is easy to change.
Also, I improved the solution now to have a callback for on img error. If the image is loaded, it is added to the array, if it fails it is not. I also changed it to utilize jquery.
浏览现有答案,我很惊讶没有人实现 基于 Promise 的解决方案在我看来是最优雅且最适合此类问题的解决方案,因此这里是一个:
Browsing existing answers I was surprised that no one has implemented a promise based solution which in my opinion is the most elegant and suitable for such a problem, so here is one: