0

Hello I'm having a trouble with my jQuery it scrolls to bottom well but I want it to scroll to bottom once not every x seconds is pretty annoying and I don't know how to fix it here is my code :)

setInterval(function(){
    var request = $.ajax({
    'url': './message/all',
    'type': 'GET',
    });

    request.error(function(status){
        $('html').html(status.responseText)
    })

    request.done(function(response){
        if(response == '')
        {
            $('.chat-body').html('No Messages :)');
        }
        else
        {

            $('.chat-body').html(response);
            jQuery("div#demo").scrollTop(jQuery("div#demo")[0].scrollHeight);

        }
    })
}, 1500)
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    As stated in [ask], you should provide as many details as possible, like for example, when do you want exactly to scroll down? after the page is loaded? after a given event? Also, the setInterval function does just that, execute code continously, at the specified interval time, why dont you take the scrolling code outside the setInterval function? – saljuama Dec 23 '15 at 20:36
  • 1
    Possible duplicate of [jQuery Scroll to bottom of page/iframe](http://stackoverflow.com/questions/1890995/jquery-scroll-to-bottom-of-page-iframe) – RisingSun Dec 23 '15 at 21:29
  • Please add commas! And if you want it to only run once why use setInterval? – Olga Dec 23 '15 at 22:26

1 Answers1

0

If you wanna do it only once, change your setInterval(function(){ to:

setTimeout(function() {

The setInterval(function, n) executes the function every n seconds, while setTimeout(function, n) executes only once, after n seconds.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252