1

Is there any way to stay on a scroll position when an ajax is in refresh interval every 3 seconds? Here is the code that is in another webpage called "sample.php":

<script type="text/javascript" src = "includes/jquery.js"></script>
<script>
    var inProcess = false;

    setInterval( function () {
        if (inProcess) {
            return false;
        }
        inProcess = true;
        jQuery.ajax({
            url: 'data.php', 
            data: '', 
            method: 'POST', 
            success: function(answer) {
                jQuery('#Load').html(answer);
                inProcess = false;
            },
            error: function() {
                inProcess = false;
            }
        });
    }, 3000 );


</script>
<div id = "Load"> </div>

Inside data.php, I just run a query to echo out table rows. The ajax in sample.php, basically sends a request to data.php every 3 seconds to "repeat" the query so when a new value enters the database, it will automatically echo out after 3 seconds. It works but I would like the scroll position to stay where it is every time the ajax reloads.

Mark
  • 11
  • 1
  • Does this answer your question? [Get scroll position using jquery](https://stackoverflow.com/questions/14886038/get-scroll-position-using-jquery) combined with [Set scroll position](https://stackoverflow.com/questions/13897603/set-scroll-position-on-page-load) – freedomn-m Jun 24 '21 at 13:32
  • try fixing the position of scrollbar through CSS. `position;fixed` – Faizan Ali Jun 24 '21 at 13:33
  • To address the X problem of your XY problem: don't poll your database and return the entire table every few seconds, as this will not scale well. Alternatives include using WebSockets or (if you want to stick with polling) only return the rows that have changed and update just those rows (either as html or as data and format in js). – freedomn-m Jun 24 '21 at 13:37
  • echo out entire table from data.php everytime, sort the rows properly, if they are pretty much dynamic do little styles using css either in data.php output or in sample.php. – Mohammed Khurram Jul 28 '21 at 09:34

0 Answers0