在我们的网站中,我将一个由flash builder制作的flash嵌入到html中,flash的大小超过2M。由于网络故障,加载闪存可能需要30秒。如何知道flash已被浏览器完全加载?
发布于 2012-11-22 03:09:14
您可以轮询SWF以获取其PercentLoaded值。
有一种方法可以做到这一点(代码复制自learnswfobject.com):
function swfLoadEvent(fn){
//Ensure fn is a valid function
if(typeof fn !== "function"){ return false; }
//This timeout ensures we don't try to access PercentLoaded too soon
var initialTimeout = setTimeout(function (){
//Ensure Flash Player's PercentLoaded method is available and returns a value
if(typeof e.ref.PercentLoaded !== "undefined" && e.ref.PercentLoaded()){
//Set up a timer to periodically check value of PercentLoaded
var loadCheckInterval = setInterval(function (){
//Once value == 100 (fully loaded) we can do whatever we want
if(e.ref.PercentLoaded() === 100){
//Execute function
fn();
//Clear timer
clearInterval(loadCheckInterval);
}
}, 1500);
}
}, 200);
}
//This function is invoked by SWFObject once the <object> has been created
var callback = function (e){
//Only execute if SWFObject embed was successful
if(!e.success || !e.ref){ return false; }
swfLoadEvent(function(){
//Put your code here
alert("The SWF has finished loading!");
});
};
swfobject.embedSWF("movie.swf", "flashcontent", "550", "400", "9", false, false, false, false, callback);发布于 2016-05-27 02:41:46
pipwerks回答运行良好。你甚至可以得到更低的值:而不是1500,只有100就可以了。
但我在Firefox上遇到了一些问题。initialTimeout应该是一个timeInterval,而不是超时,因为在FF中,有时第一次调用时你会有未定义的e.ref.PercentLoaded,但下一次调用就可以了。当然,当if为真时,您需要调用clearInterval(InitialTimeout)。
所以你会得到像这样的东西:
var initialTimeout = setInterval(function (){
if(typeof e.ref.PercentLoaded !== "undefined" && e.ref.PercentLoaded()){
clearInterval(initialTimeout);https://stackoverflow.com/questions/13490662
复制相似问题