jQuery .load(function)同步 [英] jQuery .load(function) synchronous

查看:1151
本文介绍了jQuery .load(function)同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同步使用jQuery加载函数的最佳方法是什么。

What is the best way to use the jQuery load function synchronously.

我需要加载图像但是在该图像之前无法执行下一行代码已加载。

I need to load an image but can't execute the next line of code until that image has loaded.

我可以循环变量,直到加载完成,但想知道是否有更好的方法。

I could loop a variable until the load has completed but was wondering if there was a better way of doing that.

var img = jQuery('<img src="' + url + '"/>').load(function () {                 

            });  
  //Run code here once img load has comlpeted.                                              


推荐答案

据我所知, load 事件将始终激活异步,除非图像已经缓存(在某些浏览器中)。唯一可靠的解决方案是将代码放入回调中。但是,要确保在所有浏览器中始终触发 load 处理程序,即使图像已缓存,请确保在之前添加处理程序设置图像的 src 属性。

From what I know, the load event will always fire asynchronously, except if the image is already cached (in some browsers). The only reliable solution is to put the code in a callback like you did. However, to make sure the load handler will always be fired in all browsers, even if the image is cached, make sure to add the handler before setting the src property of the image.

这篇关于jQuery .load(function)同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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