Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery/Ajax每60秒检查一次用户消息_Javascript_Ajax_Jquery - Fatal编程技术网

Javascript JQuery/Ajax每60秒检查一次用户消息

Javascript JQuery/Ajax每60秒检查一次用户消息,javascript,ajax,jquery,Javascript,Ajax,Jquery,我现在运行一个以通知为特色的网站,我想运行一个脚本,每隔60秒左右检查一次新消息。我需要做的是通过脚本传递use id,然后创建一个警报(现在我使用了一个基本的浏览器警报),但我的代码似乎不起作用。以下是我目前掌握的代码: JQuery <script type="text/javascript"> $(document).ready(function() { var check; var yourid = <?php echo json_encode($yourid); ?

我现在运行一个以通知为特色的网站,我想运行一个脚本,每隔60秒左右检查一次新消息。我需要做的是通过脚本传递use id,然后创建一个警报(现在我使用了一个基本的浏览器警报),但我的代码似乎不起作用。以下是我目前掌握的代码:

JQuery

<script type="text/javascript">
$(document).ready(function()
{
var check;
var yourid = <?php echo json_encode($yourid); ?>;
function checkForMessages() {
    jQuery.ajax({
        type: 'POST',
        url: 'php/check-messages.php',
        data: 'id='+ yourid,
        cache: false,
        success: function(response){
            if(response = 1){
                clearInterval(check);
                alert("You have mail!");
            }
            else {}
        }
    });
}
check = setInterval(checkForMessages, 60000);
};
</script>

Check Messages PHP

<?php
include("settings.php");

$id= mysqli_real_escape_string($con, $_REQUEST["id"]);

$check_messages_sql = "SELECT * FROM notes WHERE recipient = '$yourid' AND seen = '0'";
$check_messages_res = mysqli_query($con, $check_messages_sql);
if(mysqli_affected_rows($con)>0){
echo "1";
}
?>
JQuery
$(文档).ready(函数()
{
var检查;
var yourid=;
函数checkFormMessages(){
jQuery.ajax({
键入:“POST”,
url:'php/check messages.php',
数据:“id=”+yourid,
cache:false,
成功:功能(响应){
如果(响应=1){
清除间隔(检查);
提醒(“你有邮件!”);
}
else{}
}
});
}
check=设置间隔(checkForMessages,60000);
};
检查消息PHP

您可以使函数递归,并添加60秒延迟:

function checkForMessages() {

    var dataObj = {};
    dataObj['id']= yourid;

    jQuery.ajax({
        type: 'POST',
        url: 'php/check-messages.php',
        data: dataObj,
        cache: false,
        success: function(response){
            if(response == 1){
                clearInterval(check);
                alert("You have mail!");
            }
            else {}
        }
    });
    setTimeout(
      function() 
      {
        checkForMessages();
      }, 1000);

}
这样,您的功能将每1000毫秒重新启动一次。

如何

function checkForMessages(yourid) {
    jQuery.ajax({
        type: 'POST',
        url: 'php/check-messages.php',
        data: 'id='+ yourid,
        cache: false,
        success: function(response){
            if(response = 1){
                clearInterval(check);
                alert("You have mail!");
            }
            else {}
        }
    });
}
check = setInterval(checkForMessages(yourid), 60000);

尝试使用check as
if(response==1)
并设置超时,如下所示

setTimeout(function() {checkForMessages();}, 2000);

谢谢,但它仍然没有生成警报。我刚刚注意到“if(response=1)”部分,它不应该是“if(response==1)”吗?我不确定数据部分的语法。您能使用以下语法重试吗:data:{id':+yourid}$yourid是从php select中提取的,所以我用JSON将它编码成一个变量
var yourid=;
你能控制它并显示它吗?我认为jour AJAX调用中的问题是关于发送的数据。