2011-01-22 40 views
2

問題Javascript什麼時候可以開始調用Actionscript?

是否有非輪詢的方式爲Javascript命令閃存權當它的外部接口準備好了嗎?

背景

在ActionScript中,我已經註冊了一個功能的Javascript調用:

ExternalInterface.addCallback('doStuff", this.doStuff); 

我用SWFObject將Flash嵌入到我的網頁:

swfobject.embedSWF(
    'flash/player.swf', 
    'flashPlayer', 
    '100%', 
    '100%', 
    '9', 
    'expressInstallSwfTODO.swf', 
    {}, 
    {allowfullscreen: true}, 
    {}, 
    function(status) { 
     if (!status.success) { 
      alert('Failed to embed Flash player'); 
     } else { 
      $('flashPlayer').doStuff(); 
     } 
    }.bind(this) 
); 

當Flash通過回調成功嵌入時,SWFObject允許您運行代碼。我嘗試在此回調中運行$('flashPlayer')。doStuff,但它聲稱它未定義。看來Flash需要一些時間來啓動它的外部接口。所以我一直在使用輪詢攻擊來找出外部接口什麼時候準備就緒:

new PeriodicalExecutuer(
function(poller) { 
    if ($('flashPlayer').doStuff) { 
    $('flashPlayer').doStuff(); 
    poller.stop() 
    } 
}, 
0.2 
); 

這個輪詢器並不理想。在doStuff的執行過程中存在視覺上可察覺的延遲,並且它使我的整體代碼結構變得模糊。

+0

的onload會平滑但速度較慢。拿你的選擇。 – 2011-01-22 23:49:14

回答

4

在Javascript中:

function flashIsReady() 
{ 
    $('flashPlayer').doStuff(); 
} 

在ActionScript:

if (ExternalInterface.available) { 
    ExternalInterface.addCallback('doStuff', this.doStuff); 
    ExternalInterface.call("flashIsReady"); 
} 
+0

如果在運行此AS時ExternalInterface不可用,該怎麼辦? – JoJo 2011-01-23 00:27:44

0

我做了一個投票的解決方案。在動作我有這樣的功能:

private function extIsInterfaceReady():Boolean { 
    return ExternalInterface.available; 
} 

而在JavaScript中,「onFlashReady」事件我也編入intialization後,我開始的間隔是這樣的:

this.poll_flash = setInterval(function() { 
    if (typeof this.flash_obj === 'undefined') { 
     return false; 
    } 

    if (typeof this.flash_obj.isInterfaceReady === 'undefined') { 
     return false; 
    } 

    if (this.flash_obj.isInterfaceReady()) { 
     clearInterval(this.poll_flash); 
     return this.continueOn(); 
    } 
    }, 100); 
相關問題