Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS send data twice?

Tags:

javascript

I create code to get data with js but i find problem JS send data twice this my JS code :

$(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;
});

How I can fix this problem ?

like image 695
Mourad karoudi Avatar asked Mar 22 '26 18:03

Mourad karoudi


1 Answers

Try using .one()

$(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); 
});
like image 79
guest271314 Avatar answered Mar 25 '26 06:03

guest271314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!