In both examples, you are calling setInterval with a text string containing code. This code is executed in the global context, i.e. outside any function.
In your first example, alertMe() is a global function, so the call succeeds. In the second example, alertMe() is nested inside another function, so it is not defined in the global scope and the call fails. The best way to avoid this problem is to not use a text string in your setInterval call, but to use a function reference instead: $(document).ready(function() { function alertMe() { alert("Testing 123!"); }; function intervalTrigger() { setInterval( alertMe, 3000 ); }; intervalTrigger(); }); Note that you don't use alertMe(), but just alertMe - you don't want to *call* alertMe directly, you want to simply get a reference to it which you can pass to setInterval(). You'll often see an anonymous function used here: $(document).ready(function() { function intervalTrigger() { setInterval( function() { alert("Testing 123!"); }, 3000 ); }; intervalTrigger(); }); -Mike > From: skattabrain > > i'm not sure why this doesn't work ... i'm probably missing > some really important 'big picture' understanding of JS and > jQuery, but here goes ... > > This code will trigger my alert every 3 seconds if placed > outside $ (document).ready ... but if it's inside it, it fails. > > > OK, this works ... > > > function alertMe() { > alert("Testing 123!"); > }; > > function intervalTrigger() { > setInterval("alertMe()", 3000); > }; > > intervalTrigger(); > > $(document).ready(function() { > > }); > > > > > this fails ... > > > $(document).ready(function() { > > function alertMe() { > alert("Testing 123!"); > }; > > function intervalTrigger() { > setInterval("alertMe()", 3000); > }; > > intervalTrigger(); > > }); >