I start the cycle, the condition is repeated, checking the value I need, but when I get the answer true, the cycle is not reset, it gives the error "ReferenceError: timer is not defined", why? How can I solve this problem?

function sboot() ( if(условие) { clearInterval(timer); }; ); //условия для повторения function sfinboot() {var timer = setInterval(sboot, 2000);}; //функция цикла sfinboot() //запуск 

    1 answer 1

    The problem is due to the visibility of the timer variable, the sboot function does not see it, it is necessary to endure and everything should work! Do not forget about the scope of variables :)

     var timer; function sboot() ( if( условие) { clearInterval(timer); }; ); function sfinboot() { timer = setInterval(sboot, 2000); }; //функция цикла sfinboot() //запуск 
    • Or constantly transmit it - xAqweRx
    • Thank you very much, everything worked - SloGS