Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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 使用setInterval设置检索会话的间隔_Javascript_Jquery - Fatal编程技术网

Javascript 使用setInterval设置检索会话的间隔

Javascript 使用setInterval设置检索会话的间隔,javascript,jquery,Javascript,Jquery,我试图在我的函数中添加一个setInterval,使其在选择用户后自动检索我的对话 以下是我到目前为止的情况: stats_li.find("a").on("click", function() { $('.chatBox input[type="hidden"]').attr('value', $(this).data("userid")); var user = $(this).data("usernem"); var uid = $(this).data("suid

我试图在我的函数中添加一个
setInterval
,使其在选择用户后自动检索我的对话

以下是我到目前为止的情况:

stats_li.find("a").on("click", function() {
    $('.chatBox input[type="hidden"]').attr('value', $(this).data("userid"));
    var user = $(this).data("usernem");
    var uid = $(this).data("suid");
    var rid = $(this).data("userid"),
        data = {
            chat: uid,
            rid: rid,
            name: user
        };
    id = setInterval(function() {
        $.ajax({
            url: "includes/handlechat.php",
            type: "GET",
            data: data,
            dataType: 'json',

            success: function(result) {
                $("#clog").empty();
                $.each(result, function(rowKey, row) {

                    $("#clog").append('<p ><h4>' + row.username + ':</h4>' + row.message_content + '</p>');

                });
                clearInterval(id);
            }
        })
    }, 200);
});
stats\u li.find(“a”)。在(“单击”,函数()上){
$('.chatBox input[type=“hidden”]').attr('value',$(this.data(“userid”);
var user=$(this).data(“usernem”);
var uid=$(this.data(“suid”);
var rid=$(this).data(“userid”),
数据={
聊天室:uid,
里德:里德,
姓名:用户
};
id=setInterval(函数(){
$.ajax({
url:“includes/handlechat.php”,
键入:“获取”,
数据:数据,
数据类型:“json”,
成功:功能(结果){
$(“#阻塞”).empty();
$.each(结果、函数(行键、行){
$(“#clog”).append(“”+row.username+”:“+row.message_content+”

”); }); 清除间隔(id); } }) }, 200); });
我的问题是这个脚本加载
http://localhost/chat/includes/handlechat.php?chat=2&rid=1&name=a.b
取决于我在数据库中的对话数量

  • 如何使数据库中的所有对话只加载一次
  • 加载完成后,如何再次运行间隔
  • 我的聊天应用程序是否建议使用
    setInterval

有人能给我一个提示吗?

一般来说,在这里使用间隔对于异步处理来说是不好的-相反,在成功回调中设置一个新的超时是一种方法。我不明白为什么在第一个响应到达后要清除间隔?@Bergi我的想法是,如果我清除间隔,整个函数都会被清除将再次刷新,因为它在脚本中检查用户是否已登录(类似于facebook)。我的假设是否错误?抱歉,我是javascript=(不,清除间隔确实会停止重复执行,而不是重新启动循环。@Bergi那么您对我的问题有什么建议?请告诉我需要帮助=(