And to explain why that makes a difference, when you use a string in setTimeout it evaluates the code in the global context - as if it were outside all your functions. So the timeout code can't see the tabLink variable.
Using a callback function takes care of that, since a nested function can access the variables of the outer function. -Mike On Thu, Oct 8, 2009 at 4:33 PM, James <james.gp....@gmail.com> wrote: > > Try: > > setTimeout(function() { > playGlobalNewsFn(tabLink); > return false; > }, timeLag); > > > On Oct 8, 8:47 am, vmrao <maheshpav...@gmail.com> wrote: > > I have the following code. I am getting an error in the setTimeout > > function, "tabLink" is undefined. Not sure what am I doing wrong. > > > > function playGlobalNewsFn(obj) { > > obj.click(); > > > > } > > > > $('#playGlobalNews').click(function() { > > var timeLag = 1000; > > $("#tabPanel2 ul li").each(function() { > > var tabLink = $(this).find("a"); > > setTimeout("playGlobalNewsFn(tabLink); return false;", > timeLag); // > > error here > > timeLag = timeLag + 3000; > > }); > > > > }); > > > > >