Решил так:
function getCode() {
reqwest({
url: '/code.json',
method: 'get',
data: {
client_id: 'wer',
client_secret: 'asdfghjk,mnbvcxsdfghj'
},
error: function (error) {
console.log(error)
},
success: function(response) {
console.log(response);
localStorage.code = response.code;
localStorage.interval = response.interval;
getToken();
setTimeout(function() {
var id = window.setTimeout(function() {}, 0);
while (id--) {
window.clearTimeout(id);
}
getCode();
}, response.expires_in * 1000);
}
})
}
function getToken() {
reqwest({
url: '/token.json',
method: 'get',
data: {
client_id: 'wer',
client_secret: 'asdfghjk,mnbvcxsdfghj',
code: localStorage.code
},
error: function (error) {
console.log(error)
setTimeout(getToken, localStorage.interval * 1000);
},
success: function(response) {
console.log(response);
}
})
}