等待 swfobject.js 加载给了我一个无限循环
我有一个 JavaScript 脚本,它使用 SWFobject 来嵌入 Flash 播放器。当我嵌入 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;
}
我的代码检查闪存对象是否已被写入。如果没有,它会在 while 循环中重复调用 embed this.embedFlashPlayer(),直到找到包含 swf 的 div。问题是这会永远循环。如果 swfobject 未定义,我可以在 try-catch 块中做什么有什么建议吗?我 90% 确定这是因为我的脚本加载速度更快,并且在库加载之前运行 embedSwfObject 命令,但我可能是错的。我的脚本在 $(function(){...}) 命令中运行。任何关于我如何解决这个问题的理论、建议、想法将不胜感激。
I have a javascript script that uses SWFobject to embed a flash player. When i got to embed the flash player with
swfobject.embedSWF(..)
i get an error reading that swfobject is undefined. I believe this is happening because our website caches the javascript for my application but doesn't cache the swfobject.js file, so myApp.js, which calls swfobject.embedSWF(..) loads long before swfobject.js. I currently can't change what's being cached, so i came up with this work around:
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;
}
My code checks to see if the flash object has been written. If it hasn't, it calls embed this.embedFlashPlayer() repeatedly in a while loop until the the div containing the swf can be found. The trouble is that this just loops forever. Any suggestion on what i can do in the try-catch block if swfobject is undefined? I am 90% sure this is because my script loads faster and is running the embedSwfObject command before the library has loaded, but i may be wrong. My script runs in $(function(){...}) command. Any theories, suggestions, ideas as to how i can resolve this would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同时
...?使用window.setInterval
:您尝试使用
while
进行轮询。setInterval
通常用来代替 while,因为(正如您可能已经注意到的),while
会导致浏览器“挂起”。while
...? Usewindow.setInterval
:You're trying to use
while
for polling.setInterval
is usually used instead of while, because (as you may have noticed),while
causes the browser to "hang".