setInterval()无法使用ajax调用

时间:2018-03-26 14:35:42

标签: javascript php ajax setinterval

我遇到了一个奇怪的问题,我在setInterval()中使用ajax调用,但它无法正常工作。请帮忙

使用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);
                }
            });
    });    

这个问题可能对我有用,但我真的需要帮助。上面的代码写在message.php中。它还有一个分区id =“message_body”

Fetch-message.php的代码如下:

 <?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>';
         }   
     }

}
}

?>

我在控制台中收到错误 ReferenceError:未定义load_messages 请帮忙。谢谢!

2 个答案:

答案 0 :(得分:1)

setInterval不会将参数传递给它调用的函数。

limit,start,to_id因此全部为undefined

这意味着当发出Ajax请求时,PHP测试if(isset($_POST["limit"],$_POST["start"],$_POST["to_id"])){失败,因此PHP不输出任何内容。

如果你想传递那些数据,那么你必须实际传递它。

答案 1 :(得分:1)

您当前的代码正在拨打load_messages

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

然而,它找不到该功能,因为它在setInterval

内被“隐藏”了
setInterval(function load_messages(limit,start,to_id){...});},1000);

因此你得到错误:

  

ReferenceError:未定义load_messages

如果您打算在调用之后为执行load_messages添加延迟,则需要移动setInterval代码。

通常在没有load_messages的情况下定义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';
    }

  });
}

并且您的调用代码在那里添加了setInterval

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

如果你不需要延迟,那么就像你现在做的那样按照上面提到的那样宣布load_Messages,并删除周围的setInterval

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