Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 JS发送数据两次?_Javascript - Fatal编程技术网

Javascript JS发送数据两次?

Javascript JS发送数据两次?,javascript,Javascript,我使用js创建代码来获取数据 但我发现问题是JS发送数据两次 这是我的JS代码: $(document).ready(function() { function last_msg_funtion() { var ID=$(".message_box:last").attr("id"); $('#last_msg_loader').html('<img src="wait.gif">'); $.post("index.php?a

我使用js创建代码来获取数据 但我发现问题是JS发送数据两次 这是我的JS代码:

$(document).ready(function() {
    function last_msg_funtion() { 
        var ID=$(".message_box:last").attr("id");
        $('#last_msg_loader').html('<img src="wait.gif">');
        $.post("index.php?action=get&last_msg_id="+ID, function(data) {
            if (data != "") {
                $(".message_box:last").after(data);         
            }
            $('#last_msg_loader').empty();
        });
        return false;
    };  

    $(window).scroll(function() {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
           last_msg_funtion();
        }
    }); 
    return false;
});
$(文档).ready(函数(){
函数last\u msg\u funtion(){
var ID=$(“.message_box:last”).attr(“ID”);
$('last#msg_loader').html('';
$.post(“index.php?action=get&last_msg_id=“+id,函数(数据)){
如果(数据!=“”){
$(“.message_box:last”)。在(数据)之后;
}
$('last#msg_loader').empty();
});
返回false;
};  
$(窗口)。滚动(函数(){
if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()){
最后一项功能();
}
}); 
返回false;
});
如何解决此问题?

尝试使用
.one()

$(文档).ready(函数(){
函数last\u msg\u funtion(){
if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()){
var ID=$(“.message_box:last”).attr(“ID”);
$('last#msg_loader').html('';
$.post(“index.php?action=get&last_msg_id=“+id,函数(数据)){
如果(数据!=“”){
$(“.message_box:last”)。在(数据)之后;
}
$('last#msg_loader').empty();
$(窗口)。一个(“滚动”,最后一个功能0)
});             
}
$(窗口)。一个(“滚动”,最后一个功能0)
返回false;
};  
$(窗口)。一个(“滚动”,最后一个功能0);
});

scroll以极快的速度多次启动,您需要“去盎司”i如何在@dandavis?您是否必须使用$(窗口)。滚动以发送数据?这似乎是一个糟糕的做法。@grandor看到更新的帖子,添加了
$(窗口)。一(“滚动”,last\u msg\u funtion0)
之外如果
条件当页面快速滚动时出现问题,我想解决方法是让代码等待几秒钟。如果您知道如何编辑我的代码,请尝试替换大于
或小于

$(document).ready(function() {
    function last_msg_funtion() { 
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
          var ID=$(".message_box:last").attr("id");
          $('#last_msg_loader').html('<img src="wait.gif">');
          $.post("index.php?action=get&last_msg_id="+ID, function(data) {
            if (data != "") {
                $(".message_box:last").after(data);         
            }
            $('#last_msg_loader').empty();
            $(window).one("scroll", last_msg_funtion0)
          });             
        }
        $(window).one("scroll", last_msg_funtion0)
        return false;
    };  

    $(window).one("scroll", last_msg_funtion0); 
});