仅在 jquery 中按下的最后一个按钮重复单击

repeat the click only on the last button pressed in jquery

我在一个页面上有很多按钮。每个按钮都有不同的 ID。单击任何按钮后,将显示聊天信息。单击按钮后,我希望再次自动完成此操作。

问题:

如果我先点击一个按钮(开始聊天)然后点击另一个按钮,系统会点击两个按钮。但是我只想在最后一个按下的按钮上重复点击。

我用了下面的代码,但是两个按钮都点了

var intervalID = setInterval(myCallback, 3000);

function myCallback()
{
    $("#vj'.$row['id'].'").click();

}

或:

var intervalID = setInterval(myCallback, 3000);

function myCallback()
{
    $("#vj'.$row['id'].'").trigger("click");

}

完整代码:

<?php
    $query_first = "SELECT * FROM apply WHERE cgemail='$email' ";
    $results_first = mysqli_query($conn, $query_first);
    while($row = mysqli_fetch_assoc($results_first)){
        $cremail=$row['email'];
        $query_first2 = "SELECT * FROM users_cr WHERE email='$cremail' ";
        $results_first2 = mysqli_query($conn, $query_first2);
        $row2 = mysqli_fetch_assoc($results_first2);

    $acc=$row['accept'];
    if($acc==0){$show=_45;}else{$show='<b class="text-success">'._44.'</b>';}
    echo'
        <div class="mb-5">
            <div class="mb-4 just">
                <span class="fs-4 fw-bold">'.$row['title'].'</span>&nbsp;<span class="text-muted">'.$row['timesubmit'].'</span><span class="color_orange float-end pr-3 pt-3">'.$show.'</span>
                <br><br>
                <b class="text-success fw-bold">'._CARERECEIVER2.' : '.$row2['name'].'</b><br>
                '.$row['description'].' 
                <br>
            </div><br><br>
            <button id="vj'.$row['id'].'" onmousedown="func1" class="btn btn-primary float-end">'._46.'</button>
        </div>
        <hr>                                        
        <script>                                                    
            $(document).ready(function(){                                               
                
                $("#vj'.$row['id'].'").click(function(event){

                            
                var bla = '.$row['id'].';
                var requestid = '.$row['requestid'].';
                    $.ajax({
                        type: "POST",
                        url: "fetch_one_chat.php",
                        data: {bla2:bla,bla3:requestid},
                        beforeSend: function (html) {
                            
                        },
                        success: function (msg) {
                            
                            $("#one").html(msg);
                            
                            var intervalID = setInterval(myCallback, 3000);

                            function myCallback()
                            {
                                $("#vj'.$row['id'].'").click();

                            }
                            
                            
                        }
                    });
                });                                         


                    
                
            });                                                                                     
        </script>                                           
    ';
    }                                               
?>

我刚刚用 jQuery 和 Bootstrap

为您写了一个 html 代码

您只需创建一个新的 setTimeout 函数,以便在第一次单击相应按钮时再次单击同一按钮。

这将创建一个循环并让您最后点击的按钮之一自动点击。

$(function() {
  let theTimeout;
  let clickNo = 0;
  console.log("Btn function");
  $(".btn-to-click").on("click", function() {
    clickNo++;
    let btnElement = this.getAttribute("id");
    console.log("Button clicked with id " + btnElement + " => " + clickNo);
    clearTimeout(theTimeout);
    theTimeout = setTimeout(() => {
      $(`#${btnElement}`).trigger("click");
    }, 1000);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
  <button class="btn btn-primary btn-to-click" id="click-me-1">
        click me one
      </button>
  <button class="btn btn-success btn-to-click" id="click-me-2">
        click me two
      </button>
</div>