首页 > 解决方案 > 如何在函数外部的 if 语句中使用函数中的更新局部变量?JS

问题描述

我正在为我的动画使用 if 语句,但它在函数之外。

这是一个例子

    var index = 0;
    function animate() {
    index++;
    }
    if (window.index < 100) {
    setInterval(function(){
      animate();
    }, 10);
    }
window.requestAnimationFrame(animate)

但是如何获取索引以便更新函数之外的索引呢?

标签: javascriptfunctionvariablesanimationscope

解决方案


推荐阅读