douguazhi5966 2018-03-26 14:35
浏览 157

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!!

  • 写回答

2条回答 默认 最新

  • dongrao9436 2018-03-26 14:38
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?