我有一个使用SWFobject嵌入flash播放器的javascript脚本。当我用
嵌入flash播放器时swfobject.embedSWF(..)
我在读取swfobject未定义时出错。我相信这种情况正在发生,因为我们的网站为我的应用程序缓存了javascript但没有缓存swfobject.js文件,因此调用swfobject.embedSWF(..)的myApp.js在swfobject.js之前加载很久。我目前无法改变正在缓存的内容,所以我想出了这个工作:
while(!$(that.mediaPlayer).find('#'+that.playerID)[0]){
console.log(that.playerID+' not defined');
that.embedFlashPlayer(1,1);
}
...
this.embedFlashPlayer = function (width, height){
var that = this;
var playerID = that.playerID;
var server = document.URL.replace(/^.*\/\//,'').replace(/\..*$/,'');
var flashvars = {};
var flashSrc = "/flash/AS3MediaPlayer.swf?server"+server+"&playerID="+playerID;
//parameters
var params = {};
params.movie = flashSrc;
params.quality = "high";
params.play = "true";
params.LOOP = "false";
params.wmode = "transparent";
//attributes
var attr = {};
attr.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
attr.codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,16,0";
attr.width = "" + width;
attr.height = "" + height;
attr.id = playerID;
//console.log("embedding flash object with id "+playerID);
//command to embed flash object
try{
swfobject.embedSWF(flashSrc, "no-flash", width, height,"9.0.0","",flashvars,params);
}catch(err){
// I DON'T KNOW WHAT TO DO HERE
}
return true;
}
我的代码检查是否已写入flash对象。如果没有,则调用在while循环中重复嵌入this.embedFlashPlayer(),直到找到包含swf的div。麻烦的是,这只是永远循环。如果未定义swfobject,有什么建议可以在try-catch块中做什么?我90%肯定这是因为我的脚本加载速度更快,并且在加载库之前运行embedSwfObject命令,但我可能错了。我的脚本在$(function(){...})命令中运行。关于如何解决这个问题的任何理论,建议和想法都将不胜感激。
答案 0 :(得分:1)
while
...?使用window.setInterval
:
...
var interval = window.setInterval(function(){
//Code to check whether the object is ready or not.
if($(that.mediaPlayer).find('#'+that.playerID).length){
clearInterval(interval);
}
}, 100); //Each 100ms = 10 times a second.
...
您正在尝试使用while
进行投票。 setInterval
通常用于代替while,因为(您可能已经注意到),while
会导致浏览器“挂起”。