I'm having problem on auto refreshing a content in a web page











up vote
2
down vote

favorite












I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.



setInterval(function() {
$("#status").load('refresh.php');
},1000);


In my javascript, I'm automatically refreshing the #status with refresh.php every second.



But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.



Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?










share|improve this question


















  • 1




    What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
    – charlietfl
    Nov 21 at 13:40

















up vote
2
down vote

favorite












I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.



setInterval(function() {
$("#status").load('refresh.php');
},1000);


In my javascript, I'm automatically refreshing the #status with refresh.php every second.



But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.



Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?










share|improve this question


















  • 1




    What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
    – charlietfl
    Nov 21 at 13:40















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.



setInterval(function() {
$("#status").load('refresh.php');
},1000);


In my javascript, I'm automatically refreshing the #status with refresh.php every second.



But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.



Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?










share|improve this question













I'm creating a webpage that will automatically refresh a content to always get the latest data in the database. This is my code in Javascript.



setInterval(function() {
$("#status").load('refresh.php');
},1000);


In my javascript, I'm automatically refreshing the #status with refresh.php every second.



But when I inspect the element of my webpage and click on the network. It is also requesting(spamming) the refresh.php every second which is I think a bad practice.



Can you help me how to fixed this? Or can you suggest better ajax code to auto refresh a content without requesting the php file too much?







javascript php ajax database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 13:15









Gerald Manibale

347




347








  • 1




    What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
    – charlietfl
    Nov 21 at 13:40
















  • 1




    What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
    – charlietfl
    Nov 21 at 13:40










1




1




What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40






What you do to change this depends on how time sensitive these updates are. You are correct that ajax requests every second will put considerable load on server. If you need real time updates use websockets, otherwise balance need for speed with what you think is right for server. For example you may decide that doing this every 10 minutes might suffice and simply change the interval delay accordingly
– charlietfl
Nov 21 at 13:40














2 Answers
2






active

oldest

votes

















up vote
1
down vote













You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript






share|improve this answer




























    up vote
    0
    down vote













    This code will be request refresh.php later at timeouts.min every time when new (requested) value of #status is equal to current. If values are different, next timeout will be timeouts.min again.



    var timeouts={
    min:1000, // min await timeout = 1s
    max:16000 // max await timeout = 16s
    };
    var currentTo = timeouts.min;

    function setAutoRefresh( to ){
    setTimeout( function() {
    var current = $("#status").text();// or .html()
    $("#status").load('refresh.php', function() {
    // if we are load a new value
    if( current !== $("#status").text()) {
    currentTo = timeouts.min;
    // if loaded value are same to current
    } else {
    currentTo += timeouts.min; // or currentTo *= 2; for example
    if( currentTo > timeouts.max ){
    currentTo = timeouts.max;
    }
    }
    console.log('Next #status refresh after ' + currentTo + 'ms');
    // Set a new timeout
    setAutoRefresh( currentTo );
    });
    }, to);
    }

    // Set the first timeout
    setAutoRefresh( timeouts.min );





    share|improve this answer























    • this would also spam resfresh.php which i think the OP wants to avoid.
      – Mojo Allmighty
      Nov 21 at 13:31










    • No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
      – Николай Лубышев
      Nov 21 at 13:33








    • 1




      load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
      – charlietfl
      Nov 21 at 13:46












    • load() is asynchronous Thanx! Fixed.
      – Николай Лубышев
      Nov 21 at 13:49











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412882%2fim-having-problem-on-auto-refreshing-a-content-in-a-web-page%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript






    share|improve this answer

























      up vote
      1
      down vote













      You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript






        share|improve this answer












        You should probably use a websocket for this, get the latest status as data from the server and then change only that section of the page using javascript







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 13:28









        Pamicel

        686




        686
























            up vote
            0
            down vote













            This code will be request refresh.php later at timeouts.min every time when new (requested) value of #status is equal to current. If values are different, next timeout will be timeouts.min again.



            var timeouts={
            min:1000, // min await timeout = 1s
            max:16000 // max await timeout = 16s
            };
            var currentTo = timeouts.min;

            function setAutoRefresh( to ){
            setTimeout( function() {
            var current = $("#status").text();// or .html()
            $("#status").load('refresh.php', function() {
            // if we are load a new value
            if( current !== $("#status").text()) {
            currentTo = timeouts.min;
            // if loaded value are same to current
            } else {
            currentTo += timeouts.min; // or currentTo *= 2; for example
            if( currentTo > timeouts.max ){
            currentTo = timeouts.max;
            }
            }
            console.log('Next #status refresh after ' + currentTo + 'ms');
            // Set a new timeout
            setAutoRefresh( currentTo );
            });
            }, to);
            }

            // Set the first timeout
            setAutoRefresh( timeouts.min );





            share|improve this answer























            • this would also spam resfresh.php which i think the OP wants to avoid.
              – Mojo Allmighty
              Nov 21 at 13:31










            • No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
              – Николай Лубышев
              Nov 21 at 13:33








            • 1




              load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
              – charlietfl
              Nov 21 at 13:46












            • load() is asynchronous Thanx! Fixed.
              – Николай Лубышев
              Nov 21 at 13:49















            up vote
            0
            down vote













            This code will be request refresh.php later at timeouts.min every time when new (requested) value of #status is equal to current. If values are different, next timeout will be timeouts.min again.



            var timeouts={
            min:1000, // min await timeout = 1s
            max:16000 // max await timeout = 16s
            };
            var currentTo = timeouts.min;

            function setAutoRefresh( to ){
            setTimeout( function() {
            var current = $("#status").text();// or .html()
            $("#status").load('refresh.php', function() {
            // if we are load a new value
            if( current !== $("#status").text()) {
            currentTo = timeouts.min;
            // if loaded value are same to current
            } else {
            currentTo += timeouts.min; // or currentTo *= 2; for example
            if( currentTo > timeouts.max ){
            currentTo = timeouts.max;
            }
            }
            console.log('Next #status refresh after ' + currentTo + 'ms');
            // Set a new timeout
            setAutoRefresh( currentTo );
            });
            }, to);
            }

            // Set the first timeout
            setAutoRefresh( timeouts.min );





            share|improve this answer























            • this would also spam resfresh.php which i think the OP wants to avoid.
              – Mojo Allmighty
              Nov 21 at 13:31










            • No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
              – Николай Лубышев
              Nov 21 at 13:33








            • 1




              load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
              – charlietfl
              Nov 21 at 13:46












            • load() is asynchronous Thanx! Fixed.
              – Николай Лубышев
              Nov 21 at 13:49













            up vote
            0
            down vote










            up vote
            0
            down vote









            This code will be request refresh.php later at timeouts.min every time when new (requested) value of #status is equal to current. If values are different, next timeout will be timeouts.min again.



            var timeouts={
            min:1000, // min await timeout = 1s
            max:16000 // max await timeout = 16s
            };
            var currentTo = timeouts.min;

            function setAutoRefresh( to ){
            setTimeout( function() {
            var current = $("#status").text();// or .html()
            $("#status").load('refresh.php', function() {
            // if we are load a new value
            if( current !== $("#status").text()) {
            currentTo = timeouts.min;
            // if loaded value are same to current
            } else {
            currentTo += timeouts.min; // or currentTo *= 2; for example
            if( currentTo > timeouts.max ){
            currentTo = timeouts.max;
            }
            }
            console.log('Next #status refresh after ' + currentTo + 'ms');
            // Set a new timeout
            setAutoRefresh( currentTo );
            });
            }, to);
            }

            // Set the first timeout
            setAutoRefresh( timeouts.min );





            share|improve this answer














            This code will be request refresh.php later at timeouts.min every time when new (requested) value of #status is equal to current. If values are different, next timeout will be timeouts.min again.



            var timeouts={
            min:1000, // min await timeout = 1s
            max:16000 // max await timeout = 16s
            };
            var currentTo = timeouts.min;

            function setAutoRefresh( to ){
            setTimeout( function() {
            var current = $("#status").text();// or .html()
            $("#status").load('refresh.php', function() {
            // if we are load a new value
            if( current !== $("#status").text()) {
            currentTo = timeouts.min;
            // if loaded value are same to current
            } else {
            currentTo += timeouts.min; // or currentTo *= 2; for example
            if( currentTo > timeouts.max ){
            currentTo = timeouts.max;
            }
            }
            console.log('Next #status refresh after ' + currentTo + 'ms');
            // Set a new timeout
            setAutoRefresh( currentTo );
            });
            }, to);
            }

            // Set the first timeout
            setAutoRefresh( timeouts.min );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 14:22

























            answered Nov 21 at 13:30









            Николай Лубышев

            40337




            40337












            • this would also spam resfresh.php which i think the OP wants to avoid.
              – Mojo Allmighty
              Nov 21 at 13:31










            • No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
              – Николай Лубышев
              Nov 21 at 13:33








            • 1




              load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
              – charlietfl
              Nov 21 at 13:46












            • load() is asynchronous Thanx! Fixed.
              – Николай Лубышев
              Nov 21 at 13:49


















            • this would also spam resfresh.php which i think the OP wants to avoid.
              – Mojo Allmighty
              Nov 21 at 13:31










            • No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
              – Николай Лубышев
              Nov 21 at 13:33








            • 1




              load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
              – charlietfl
              Nov 21 at 13:46












            • load() is asynchronous Thanx! Fixed.
              – Николай Лубышев
              Nov 21 at 13:49
















            this would also spam resfresh.php which i think the OP wants to avoid.
            – Mojo Allmighty
            Nov 21 at 13:31




            this would also spam resfresh.php which i think the OP wants to avoid.
            – Mojo Allmighty
            Nov 21 at 13:31












            No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
            – Николай Лубышев
            Nov 21 at 13:33






            No! its spam only if requested value is diffrent then current. If value is same, this code make request more and more later after previous
            – Николай Лубышев
            Nov 21 at 13:33






            1




            1




            load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
            – charlietfl
            Nov 21 at 13:46






            load() is asynchronous and this code assumes it is not. You should also provide a proper explanation of what it does and how it solves OP's issues
            – charlietfl
            Nov 21 at 13:46














            load() is asynchronous Thanx! Fixed.
            – Николай Лубышев
            Nov 21 at 13:49




            load() is asynchronous Thanx! Fixed.
            – Николай Лубышев
            Nov 21 at 13:49


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412882%2fim-having-problem-on-auto-refreshing-a-content-in-a-web-page%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...