异步还是同步?当我们设置Image对象的src属性时? [英] async or sync ? when we set the src property of the Image object?

查看:77
本文介绍了异步还是同步?当我们设置Image对象的src属性时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var img=new Image();
img.src='xxxxx';

浏览器会等待图像加载然后执行下一条代码行吗?

Will the browser wait for the image to load then execute the next code line?

推荐答案

该操作是异步的;许多图像预加载"代码都依赖于该功能.

That action is asynchronous; a lot of image 'pre-loading' code relies on that feature.

还要提供更多有用的信息.如果您希望同步执行某些操作以等待通过javascript的图像对象加载图像,则可以使用onload事件,如下所示:

To give a little bit more useful information as well. If you're wanting to have certain actions synchronously wait for images to load via javascript's image object, you can use the onload event, like so:

var img = new Image();
img.onload = function () { /* onLoad code here */ };
img.src = 'xxxxxx';

这篇关于异步还是同步?当我们设置Image对象的src属性时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆