This question has already been answered:

setInterval running, in the interval it clicks on a button, it sends a specific request with clicks, in response to a click we get answers to requests, how to reset setInterval receiving a specific answer?

Suppose when you receive such a response -

 success: false error: Attempting mobile confirmation: success 

We send request so -

 let confirmButton = document.querySelector('#confirmButton'); let success = setInterval(() => { confirmButton.click(); }, 3000); 

Request Button -

 <button class="btn btn-success" id="confirmButton" data-tid="0">Check Status</button> 

Reported as a duplicate by ReinRaus , Dmitriy Simushev , Igor , PashaPash 17 May '16 at 19:04 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • why in the interval? do so: if the answer is such as is necessary - stop the interval - Grundy
  • I just don't know how to work with requests and responses from the server, I wanted to ask you an example - SloGS
  • it is necessary to add the code as the request is sent - Grundy
  • And JS code where? - Dmitriy Simushev
  • setInterval is running, in the interval it clicks on a button, it sends a specific request with clicks - this code is needed - Grundy

1 answer 1

Maybe you should do it yourself, and not in pieces to make others do it ?

 // Помогает установить контроль над исполнением let flag = true; function clickHandler(){ /*fetch('/some').then(json => json.json().then(result => { // Тут проверяем какой ответ и решаем - нужно ли остановить таймер // Если нужно остановить цикл, ставим флаг (flag) в false }));*/ // Для теста let rand = Math.random(); console.info(`Click, rand ${rand}, ${rand > 0.5 ? 'change' : 'continue'}`); rand > 0.5 ? flag = false : null; // 50/50 } document.addEventListener('DOMContentLoaded', e => { // После загрузки DOM, ищем кнопку и вешаем обработчик let confirmButton = document.querySelector('#confirmButton'); confirmButton.addEventListener('click', clickHandler); // Запускаем цикл loop(confirmButton); }); function loop(btn){ if(flag){ // Если флаг позволяет, ставим таймер на клик в будущем и производим его сейчас setTimeout(() => loop(btn), 3000); btn.click(); } } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <button class="btn btn-success" id="confirmButton" data-tid="0">Check Status</button> 

  • I do it myself, before asking, I look for travel information on the Internet, and if I don’t find anything, I have to ask, and I need an autoclicker with a number of actions, so I always have to come to this site and ask knowledgeable people, you don’t even have to write all the code for me, but just tell in which direction to go and what functions to dig in - SloGS
  • @SloGS, but after all it is obvious - we start, check, if necessary, stop. Although maybe I'm a genius? :) - user207618
  • Most likely you are a genius, I know what to start, check, if it came to stop, I know how to start, I know how to stop, and I don’t know how to compare the answer to the request with the desired value for the condition (((((( SloGS
  • @SloGS,? if(response === 'something') stopTimer() or something like that. - user207618
  • How to build a condition, I know, but I have a question, how to get data from the answer for the condition? - SloGS