View Full Version : setTimeout function problem

08-19-2005, 02:32 AM
I have coded a function Blink(row,col) that uses the setTimeout function to alternately display two images.

function Blink(row,col){
var blinkId="ex"+row.toString()+col.toString();
var imgName=document.getElementById(blinkId).src;
var indx=imgName.indexOf("blank");
var imgNum=(indx==-1)? 0:3;

The routine works, but it only works because one of the original statements has been commented out and I added the last three statements. If I use the original code I get an error telling me that 'row' is not defined. What am I missing in my understanding of JavaScript?
Thanks for your wisdom.

08-19-2005, 06:43 AM
The problem was that row and col are parameters to your function and they don't exist in the context that Blink is called from 700ms later. When you set Xrow and Xcol to row and col respectively you created new global variables (actually properties of the window object) that could be used in the context that Blink is called from.

You could also have done it like this:


That way you don't create unnecessary global variables.

08-19-2005, 10:32 AM
note that, using global variables you may use as parameters even objects, arrays, aso., while using the local passed parameter I guess that you may pass strings only.

08-19-2005, 05:03 PM
Mongus & Kor,

Thank you for your assistance. I really wanted to call the setTimeout() function without declaring global variables. I hadn't realized that the parameter in setTimeout was a string that contains JavaScript code. Changing the statement to window.setTimeout("Blink("+row+","+col+")",700); solved the problem.