@alexsteadfast
I am noob

Как выйти из setTimeout?

Мне необходимо, при ошшибке axios выходить из функции setTimeou, моя попытка с присваиванием значение в блоке ошибки и остановка по условию не сработала.
function Axios(i,userForm, responseMain) {
    const axios = require("axios");
    var timerStop;
    var timeOutVar = setTimeout(() => {
            var str = "https://api.vk.com/method/users.get?user_ids="+i+"&fields=bdate,has_photo,has_mobile,contacts,photo_id,photo_100,photo_400_orig,home_phone,mobile_phone,personal,city&access_token=78011b4978011b4978011b49a67865f1a77780178011b49232f0e29c1c81533f40a62fc&v=5.80";
             axios.get(str).then(function (response) {    
                         console.log(response.data.response[0].first_name);
                         if(response.data.response[0].first_name != 'DELETED'){
                              console.log('NoDELETED');
                         }
                         if(response.data.response[0].first_name == userForm){
                             console.log("SAVED");
                              var res = new main();
                              res.insert(response.data.response[0].first_name , response.data.response[0].last_name , response.data.response[0].city.title ,  response.data.response[0].photo_100);
                              //res.insert(response);
                         }
                       }).catch(error => {
                       console.log(error);
                       console.log('clearTimeout');
                         
                          timerStop = 'STOP';
                        });                       
        Axios(++i);
    }, 2000);
    if(timerStop == 'STOP') clearTimeout(timeOutVar);
    
}
  • Вопрос задан
  • 284 просмотра
Пригласить эксперта
Ответы на вопрос 1
@alexhovansky
Вообще, согласен с комментарием Антон Спирин
Касаемо вопроса - а зачем "выходить из setTimeout"? Он на то и timeout, а не interval, что выполняется однажды.
По коду - он неверное написан.
На момент, когда выполняется строка
if(timerStop == 'STOP') clearTimeout(timeOutVar);
timerStop === undefined
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы