当前位置:网站首页>Functions in setinterval cannot have parentheses

Functions in setinterval cannot have parentheses

2022-06-24 07:02:00 Haitian Eagle

while The cycle will get stuck CPU, To use  setInterval Instead of .

​​​​​​whttps://jingyan.baidu.com/article/b2c186c86c3590c46ef6ffe5.htmlhttps://jingyan.baidu.com/article/b2c186c86c3590c46ef6ffe5.html

  Only once

<script>
var i=0;
function say() {
    i++;
    document.write(i+'<br>');
    if (i==10)
        clearInterval(id);
}
var id = setInterval(say(), 100);
</script>

Change to

<script>
var i=0;
function say() {
    i++;
    document.write(i+'<br>');
    if (i==10)
        clearInterval(id);
}
var id = setInterval(say, 100);
</script>

setInterval Method is executed only once after the parameter is extracted

原网站

版权声明
本文为[Haitian Eagle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240025131490.html