tags:

views:

94

answers:

3

Sorry I am new to javascript. I am trying to call an object from inside a function to allow me to get varible from a flash file at set intervals. 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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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. Thanks in advance.

A: 

looks like a javascript scope problem, see this previous solution.

http://stackoverflow.com/questions/237350/javascript-how-to-solve-var-out-of-scope-within-settimeout-call

Its worth reading up on javascript scope and save yourself a headache later on

gum411
A global variable cannot be out of scope.
Andy E
A: 

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.

Your source is also missing a ' on the following line:

var tag = new FlashTag('world.swf?data_file=data.xml, 600, 325); 
Andy E
opps, my mistake... that was caused by me tidying my code up for presentation on here :P Thanks anyway :)
millard
A: 

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. I just replaced

checkmap();

with another

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

Thanks everyone anyway

millard