setTimeout stack over flow.. $(document).ready(function(){ counterFN();
var theCounter = 1; function counterFN() { $(".searchInput").val(theCounter); theCounter++; setTimeout(counterFN(),1000); } }); </script> </head> <body> <input type="text" class="searchInput" /> </body> </html>
views:
57answers:
2
+1
A:
Change this...
setTimeout(counterFN(),1000);
to this:
setTimeout(counterFN,1000);
Otherwise, you try to call counterFN and set a timeout for whatever it returns, instead of setting a timeout for the function itself - but since it tries to call itself before returning (in your original code), this creates an infinite loop of calls, resulting in a stack overflow.
Amber
2010-05-19 09:00:20
+4
A:
You are calling counterFN and setting its return value to run after 1000 milliseconds. Since you aren't returning a function, you probably don't want to do that.
You probably want:
setTimeout(counterFN,1000);
Better yet, don't be recursive, and do more caching of things that won't change:
var theCounter = 1;
var input = $(".searchInput"); // Cache this
function counterFN()
{
input.val(theCounter);
theCounter++;
}
setInterval(counterFN, 1000);
David Dorward
2010-05-19 09:00:22
thank you sir.. you got my point without me asking the question.. :) setTimeout(counterFN,1000); really works..
vrynxzent
2010-05-19 09:04:00
@user344862: Don't make this a habit. Always clearly describe your problem, not everyone of us likes to go through code and has to *guess* what is wrong. **If you like this answer, *accept* it by clicking on the green tick next to this answer** .
Felix Kling
2010-05-19 09:21:03