Loading images synchronously with javascript

后端 未结 3 1149
清歌不尽
清歌不尽 2021-01-04 21:48

I need to load an image synchronously, that is I need to pause execution of javascript until the image is loaded. Since image.onload is asynchronous, the main function will

3条回答
  •  时光说笑
    2021-01-04 22:07

    Can't this be solved by refactoring?

    Wrap whatever code you need to execute after the image load into a function invoked by the callback (or make that function the callbak).

    I see that you say that if the main function returns, other code will execute. Can you wrap that, too? Can you delay it's execution or make it dependent on this onload event or someother event?

    You could even dynamically load the source of the other javascript code in the image onload.

提交回复
热议问题