I am trying to make an auto click on a certain button `y until Success appears on the page.

if(!$( "div:contains('Успех')" ).length) { $('button').click(); } 
  <button class="btn btn-danger btn-lg btn-block betButton" data-lower="1" data-upper="7">1 to 7</button> <button class="btn btn-success btn-lg btn-block betButton" data-lower="0" data-upper="0">0</button> <button class="btn btn-inverse btn-lg btn-block betButton" data-lower="8" data-upper="14">8 to 14</button> 

I wanted to do this in the form of a time slot but he even after the appearance of the inscription will check the page for the word How can this be done differently? And besides, I can not figure out how to distinguish the buttons from each other, that would not click on everything at once but on one if there are a lot of them.

    1 answer 1

     var div = $('div'), // элемент(ы), где ожидается появление фразы button = $('button:eq(1)'), // к примеру, клик по второй кнопке timer = setInterval(function() { if (div.filter(':contains("Успех")').length) { clearInterval(timer); // если фраза появилась, то останавливаем таймер console.log('Приехали'); return false; } button.trigger('click'); }, 1000); button.on('click', function() { console.log('Клик'); }); setTimeout(function() { div.text('Успех'); }, 5000); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button class="btn btn-danger btn-lg btn-block betButton" data-lower="1" data-upper="7">1 to 7</button> <button class="btn btn-success btn-lg btn-block betButton" data-lower="0" data-upper="0">0</button> <button class="btn btn-inverse btn-lg btn-block betButton" data-lower="8" data-upper="14">8 to 14</button> <div></div> 

    • The browser complains about the first line. Uncaught ReferenceError: $ is not defined - Roma Romin