setInterval()无法使用ajax调用[关闭]

I am stuck in a weird problem I am using ajax call inside setInterval() but it's not working. Please help

Javascript:

$(document).ready(function(){
        var limit = 25;
        var start = 0;
        var action = 'inactive';
        var to_id = "<?php echo $_GET['id']; ?>";

            setInterval(function load_messages(limit,start,to_id)
            {
                $.ajax({
                    url : "fetch-messages.php",
                    method : "POST",
                    data : {limit:limit,start:start,to_id:to_id},
                    cache : false,
                    success : 
                    function(data)
                    {
                        $('#message_body').append(data);
                        if(data=='')
                            action = 'active';
                        else
                            action = 'inactive';
                    }

                });
            },1000);

        if(action == 'inactive'){
            action = 'active';
            load_messages(limit,start,to_id);
        }
        var height_load = ($('#message_body').height());
       $('#message_body').scroll(function(){
                if(($('#message_body').scrollTop())>height_load && (action=="inactive")){
                    height_load=height_load*1.5;
                    action = 'active';
                    start = start + limit;
                    setTimeout(function(){
                        load_messages(limit,start,to_id)

                    },100);
                }
            });
    });    

This problem could be specific to me but I really need help. The above code is written in message.php. which also has a division id="message_body"

And the code for Fetch-message.php is below:

 <?php

if(isset($_POST["limit"],$_POST["start"],$_POST["to_id"])){
$dbc = mysqli_connect($hn,$un,$pd,$db);
$start = $_POST["start"];
$limit = $_POST["limit"];
$to_id=$_POST["to_id"];
$from_id = $uid;
$to_id_details = mysqli_fetch_assoc(mysqli_query($dbc,"SELECT * FROM public_signup WHERE id='$to_id'"));
$message_sent_number=mysqli_num_rows(mysqli_query($dbc,"SELECT * FROM message WHERE to_id='$to_id' and from_id='$from_id'"));
$message_recieved_number=mysqli_num_rows(mysqli_query($dbc,"SELECT * FROM message WHERE to_id='$from_id' and from_id='$to_id'"));
if($message_sent_number!=0 || $message_recieved_number!=0){
$query = "SELECT * FROM message ORDER BY id DESC LIMIT $start, $limit";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result)){
         $db_to_id = $row['to_id']; 
         $db_from_id = $row['from_id'];
         if($to_id==$db_to_id && $from_id==$db_from_id){
                                echo '<div id="new_line"><div class="individual_message_reciever">
                                <p><span id="message_name">'.ucfirst($first_name)." ".ucfirst($last_name).':</span>
                                '.nl2br($row['message']).'</p>
                                </div></div>';
         } else if($from_id==$db_to_id && $to_id==$db_from_id){
                                echo '<div id="new_line"><div class="individual_message_sender">
                                <p><span id="message_name">'.ucfirst($to_id_details['first_name'])." ".ucfirst($to_id_details['last_name']).':</span>
                                '.nl2br($row['message']).'</p>
                                </div></div>';
         }   
     }

}
}

?>

I am getting error in console ReferenceError: load_messages is not defined Please help. THANKS!!

setInterval doesn't pass arguments to the function it calls.

limit,start,to_id are therefore all undefined

This means that when the Ajax request is made, the PHP test if(isset($_POST["limit"],$_POST["start"],$_POST["to_id"])){ fails, so the PHP outputs nothing.

If you want to pass that data then you have to actually pass it.

Your current code is making a call to load_messages:

if (action == 'inactive') {
  action = 'active';
  load_messages(limit, start, to_id);
}

However, it cannot find the function as it is "hidden" away inside your setInterval

setInterval(function load_messages(limit,start,to_id){...});},1000);

Hence you get the error:

ReferenceError: load_messages is not defined

If your intend is to add a delay to the execution of load_messages after is has been called you need to move the setInterval code.

Define load_messages normally without the setInterval:

function load_messages(limit, start, to_id) {
  $.ajax({
    url: "fetch-messages.php",
    method: "POST",
    data: {
      limit: limit,
      start: start,
      to_id: to_id
    },
    cache: false,
    success: function(data) {
      $('#message_body').append(data);
      if (data == '')
        action = 'active';
      else
        action = 'inactive';
    }

  });
}

and your calling code add the setInterval there instead:

if (action == 'inactive') {
  action = 'active';
  setInterval(function() {
    load_messages(limit, start, to_id);
  }, 1000);
}

If you don't need the delay then just call it as you currently do ones you have declared load_Messages as mentioned above, removing the surrounding setInterval:

if (action == 'inactive') {
  action = 'active';
  load_messages(limit, start, to_id);
}