A
A
Alexander Ivanov2019-08-26 21:23:37
Node.js
Alexander Ivanov, 2019-08-26 21:23:37

How to get out of setTimeout?

I need to exit the setTimeou function when an axios error occurs, my attempt to assign a value in the error block and stop by condition did not work.

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);
    
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexhovansky, 2019-08-27
@alexhovansky

In general, I agree with Anton Spirin
's comment Regarding the question - why "leave setTimeout"? It is for that and timeout, not interval, which is performed once.
According to the code - it is incorrectly written.
At the moment when the line is executed timerStop === undefined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question