Click the Actions button of each Row of the table every 2 seconds

Asked

Viewed 58 times

-1

I have a table with 4 columns, and the last one a "Send" action button, where it will trigger an email.

I need to create a loop where every 2 seconds Jquery clicks the Send from a Row action. After 2 seconds the click would be on the next Row and so on...

Below the table mounted:

<table class="table table-striped table-hover table-sm">
   <tr class="bg-dark text-light">
      <th class="hidden-xs">Nome</th>
      <th>Email</th>
      <th class="text-center" style="width: 1em;">Marcar</th>
      <th class="text-center" style="width: 1em;">Ação</th>
    </tr>
    <?php
      $count = 0;
      foreach($result as $row) {
        $count = $count + 1;
          echo '
            <tr>
                <td class="hidden-xs">'.$row["customer_name"].'</td>
                <td>'.$row["customer_email"].'</td>
                <td class="text-center">
                   <label class="label--checkbox">
                     <input type="checkbox" name="single_select" class="single_select checkbox" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" />
                   </label>
                </td>
                <td>
                   <button type="button" name="email_button" class="btn btn-gray rounded btn-xs email_button btn-row-acao-enviar" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Enviar</button>                                    
               </td>
           </tr>
          ';
         }
      ?>
</table>
  • 2

    Important to post a [mcve] of what you tried and describe what difficulty found.

  • I know the rules, but I didn’t know where to start. Note that I posted a minimal example of what I did!

1 answer

1


//Indice para guardar em qual linha estamos,
//começa pelo 2 porque a primeira linha é do cabeçalho
let index = 2;

//Timer que será executado depois de 2s
setTimeout(function click() {
  //Pega o botão do índice atual
  const button = $(`table tr:nth-child(${index}) button`)

  //Se houver o botão
  if (button.length > 0) {
    //Faz o click
    button.click();
    //Incrementa o indice
    index++;
    //Reinicia o timer
    setTimeout(click, 2000);
  }
}, 2000);
  • Good @Costamilam! I tried here from your solution, but he is only clicking on the action of the first Row, after the 2s he is not clicking on the action of the next Row... Thanks in advance for your contribution and willingness to help.

  • 1

    @Andréalbson forgot to increase the index, I’ve updated the answer

  • Excellent! Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.