Every once in a while, a web programmer runs into a situation where he/she has to refresh the page to load latest data into MySQL database.

It could be because of a API limit, it could be because he has limited rights on his development machine or a development server, it could even be because he doesn’t want to go into the whole windows scheduler program out of laziness (yes, us programmers are inherently lazy).

Whatever be the reason, the best idea is refresh the page through Javascript using a simple script.

Put this at the end of your Cronjob script and run Cronjob once through your Browser (don’t close the tab after it completes its runs.)

<script>

        // self executing function here

          (function() {

          // your page initialization code here

         // the DOM will be available here

           setTimeout(function() {

             window.location.reload();
             }, 5*60*1000);   //will refresh the page every 5 minutes
            })();
 </script>

The beauty of this code is that it doesnt require any fancy libraries to run and can be used in any programming language.

A word for Pagination:

Now, onto another issue facing programmers is that the programmers don’t know how to refresh the page to keep traversing/updating newer records instead of the same records.

Since we are going to be reloading the same URL (although you can rig the script above to include pagination); The pagination can come from two sources, namely sessions and cookies.

I would recommend cookies because in sessions restarting the machine or coming back after a few days might have expired your starting point whereas a long-date expire cookie will keep the pointer intact.

Tags: , ,