我正在尝试使用 jquery 在客户端每秒增加一个值
这就是所做的:
<script type="text/javascript">
$(document).ready(function increment(){
$("#counter").text(parseInt($("#counter").text())+1);
setTimeout(increment(),1000)
})
</script>
这没有按预期工作,我收到“太多递归”错误。
对此有什么想法吗?
最佳答案
尝试
$(document).ready(function () {
function increment(){
$("#counter").text(parseInt($("#counter").text())+1);
setTimeout(increment,1000);
};
increment();
});
对了,还有setInterval()
以设定的时间间隔重复调用函数,而不是使用 setTimeout()
$(document).ready(function () {
var interval = setInterval(increment,1000);
});
function increment(){
$("#counter").text(parseInt($("#counter").text())+1);
}
关于jquery - Jquery 的 setTimeout 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1786940/