我正在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