簡體   English   中英

Javascript無法在函數內調用對象

[英]Javascript can't call an object inside a function

抱歉,我是javascript新手。 我正在嘗試從函數內部調用對象,以允許我按設置的時間間隔從Flash文件中獲取變量。 由於某種原因,該對象無法在計時器函數中運行。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>DIY Map</title>

<style>
 * { margin:0; padding:0; }
</style>

</head>
<body style="font-family:verdana;color:#999; background-image:url('bg.png'); background-repeat:no-repeat;">

<script type="text/javascript" src="js/JavaScriptFlashGateway.js"></script>
<script type="text/javascript" src="js/Exception.js"></script>
<script type="text/javascript" src="js/FlashTag.js"></script>
<script type="text/javascript" src="js/FlashSerializer.js"></script>
<script type="text/javascript" src="js/FlashProxy.js"></script>



<script type="text/javascript">
 var uid = new Date().getTime();
 var flashProxy = new FlashProxy(uid, 'js/JavaScriptFlashGateway.swf');
    var tag = new FlashTag('world.swf?data_file=data.xml, 600, 325);
    tag.setFlashvars('lcId='+uid);
    tag.write(document);


</script>

//flashProxy.call works here:
<p><a href="javascript:flashProxy.call('zoomOut');">Zoom Out</a>

<a href="javascript:flashProxy.call('refreshData','other_data.xml');">Get new data</a>

<p><a href="javascript:flashProxy.call('getZoom');">getZoom</a> | <a href="javascript:flashProxy.call('getCoords');">getCoords</a></p>



<script type="text/javascript">
// Start the refreshing process

var seconds = 3;
var zoom;
var coords;

//timer loop
function checkmap()
{
//flashProxy doesn't work here
flashProxy.call('getCoords');     
flashProxy.call('getZoom');   
alert (coords);
alert (zoom);    


setTimeout('checkmap()',seconds * 1000);
}
checkmap();



//Returns results here:
function gotCoords(n)
{
    coords = n;
}
function gotZoom(n)
{
    zoom = n;   
}
</script>

為了澄清,我試圖讓flashProxy.call('****')checkmap()函數中工作。 提前致謝。

我發現了問題...這是因為沒有計時器來啟動初始Flashproxy.call在加載Flash之前正在執行。 我剛更換

checkmap();

和另外一個

setTimeout('checkmap()',seconds * 1000); 

還是謝謝大家

看起來像是javascript范圍問題,請參閱此先前的解決方案。

如何在setTimeout調用中解決超出范圍的Var

值得閱讀有關JavaScript范圍的內容 ,以后可以避免頭痛

您是否知道您的源代碼中有多余/未封閉的腳本標簽? 這會引起問題。

<script type="text/javascript"> // This is your opening tag




<script type="text/javascript"> // Oops, this is parsed by the script engine
 var uid = new Date().getTime(); 
 var flashProxy = new FlashProxy(uid, 'js/JavaScriptFlashGateway.swf'); 
    var tag = new FlashTag('world.swf?data_file=data.xml, 600, 325); 
    tag.setFlashvars('lcId='+uid); 
    tag.write(document); 


</script> 

上面的代碼將在任何JavaScript引擎中引發語法錯誤,並停止進一步執行。

您的來源在以下行上也缺少'

var tag = new FlashTag('world.swf?data_file=data.xml, 600, 325); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM