setTimeout stack over flow
setTimeout stack over flow.. $(document).ready(function(){ counterFN();
var t开发者_开发问答heCounter = 1; function counterFN() { $(".searchInput").val(theCounter); theCounter++; setTimeout(counterFN(),1000); } }); </script> </head> <body> <input type="text" class="searchInput" /> </body> </html>
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);
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.
精彩评论