简体   繁体   English

Javascript无法在函数内调用对象

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

Sorry, I am new to javascript. 抱歉,我是javascript新手。 I am trying to call an object from inside a function to allow me to get a variable from a flash file at set intervals. 我正在尝试从函数内部调用对象,以允许我按设置的时间间隔从Flash文件中获取变量。 For some reason the object is not working inside the timer function. 由于某种原因,该对象无法在计时器函数中运行。

<!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>

To clarify, I am trying to get the flashProxy.call('****') to work in the checkmap() function. 为了澄清,我试图让flashProxy.call('****')checkmap()函数中工作。 Thanks in advance. 提前致谢。

I found the problem... it was that because there was no timer to start the inital flashproxy.call it was executing before the flash was loaded. 我发现了问题...这是因为没有计时器来启动初始Flashproxy.call在加载Flash之前正在执行。 I just replaced 我刚更换

checkmap();

with another 和另外一个

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

Thanks everyone anyway 还是谢谢大家

looks like a javascript scope problem, see this previous solution. 看起来像是javascript范围问题,请参阅此先前的解决方案。

How to solve Var out of scope within setTimeout call 如何在setTimeout调用中解决超出范围的Var

Its worth reading up on javascript scope and save yourself a headache later on 值得阅读有关JavaScript范围的内容 ,以后可以避免头痛

Did you know you have an extra/unclosed script tag in your source? 您是否知道您的源代码中有多余/未封闭的脚本标签? This would cause problems. 这会引起问题。

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

The above code would throw a syntax error in any javascript engine and halt further execution. 上面的代码将在任何JavaScript引擎中引发语法错误,并停止进一步执行。

Your source is also missing a ' on the following line: 您的来源在以下行上也缺少'

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