等待 swfobject.js 加载给了我一个无限循环

发布于 2024-12-08 01:23:58 字数 1803 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-15 01:23:58

同时 ...?使用 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 会导致浏览器“挂起”。

while ...? Use 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.
...

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文