gpt4 book ai didi

JavaScript 计数不起作用 : doubling instead of adding up

转载 作者:行者123 更新时间:2023-11-28 15:54:02 25 4
gpt4 key购买 nike

请参阅我的示例。该脚本应将变量设置为零,然后每秒加 1。相反,该值每秒都会翻倍。有人知道出了什么问题吗?

http://jsfiddle.net/kdKyz/

<script>
function count_it()
{
window.counts = window.counts+1;
$('.result').html(window.counts);
window.setInterval(count_it, 1000);
}
$('document').ready(function(){
window.counts = 0;
count_it();
});
</script>

最佳答案

count_it() 不断调用 setInterval() 并将其自身作为参数,因此不断将值加倍。您需要将 ready() 中的 count_it() 替换为 window.setInterval(count_it, 1000) 并删除 window.setInterval( count_it, 1000) 来自 count_it()

关于JavaScript 计数不起作用 : doubling instead of adding up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19484648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com