-->

How should I judge that the flash has been loaded

2019-09-03 02:41发布

问题:

In our website, I embed a flash produced by flash builder in html , and the size of flash is more than 2M. Because of the bad network, it may be spent 30 seconds to load the flash. How should i know the flash has been loaded completely by browser?

回答1:

You can poll the SWF to get its PercentLoaded value.

Here's one way to do it (code copied from 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);


回答2:

pipwerks answer is working fine. you can even get lower value: instead of 1500 only 100 will be ok.

but i experienced some issues on Firefox. instead of a timeout, initialTimeout should be a timeInterval, because on FF sometimes, at first call you'll have e.ref.PercentLoaded undefined, but next call will be fine. of course you need to call clearInterval(initialTimeout); when if is true.

so you'll get somehting like:

    var initialTimeout = setInterval(function (){
     if(typeof e.ref.PercentLoaded !== "undefined" && e.ref.PercentLoaded()){
         clearInterval(initialTimeout);