我正在JS中加载图像并等待其onload事件的状态。 我在HTML中包含了源文件中的JS文件,并且想知道最初使用JS加载的图像的状态。如何在同步Javascript中查找异步图像状态
例子:
<html> <head> <script> var imgStatus = false; var img = new Image(); img.onload = function(){ imgStatus = true; } img.src = "http://mydomains.com/someimage.jpg"; </script> </head> <body> <script type="text/javascript" src="js1.js"></script> </body> </html>
为js1.js的代码是
window.onload = function(){ // check for imgStatus = true and then only do something // till then do nothing }
我需要使用setInterval的不断检查imgStatus的价值或是否有任何其他更好的办法等待状态已知。
你不能动“新的图像()“代码到js1.js? – 2014-11-20 17:37:35
您何时/何时设置了img的src'-attribute? – 2014-11-20 17:38:36
@PrabuRaja没有那根据我的要求不可能,因为在这种情况下,JS文件加载将需要额外的时间,然后我将不得不触发图像,这将导致额外的等待像素。在目前的情况下,我的图像将加载异步虽然image.onload不会触发,直到JS准备好 – swapnilsarwe 2014-11-20 18:05:19