saurabh behl saurabh behl - 1 month ago 9
Ajax Question

triger ajax call after every 24 hours in jquery

how can i trigger Jquery ajax call after the every 24 hours?

setInterval(function() {
$.post('get_sales.php', function(data) {
$('#updates_contents').html(data);
});
}, 5000);

Answer

If you want to stick with jQuery and PHP, you can do the following:

//var intervalTime = 24 * 60 * 60 * 1000; //refresh every 1 day
var intervalTime = 1 * 60 * 60 * 1000; //refresh every 1 hour
//the format is HOUR * MINUTE * SECONDS * MILI-SECOND.

function refresher() {
     $.post('get_sales.php', function(data) {
        $('#updates_contents').html(data);
    });
}
var timerID;
function start() {
     //you might want to refresh the content when the widget first loads then uncomment the following line
     //refresher();
     //and then set interval to refresh it later on
     timerID = setInterval("refresher", intervalTime );
 }

//if something went wrong, user can click on refresh button which can call this function
function refreshBtnClick() { 
      clearTimeout(timerID ); 
      start();
}
start();

Remember to add a button to manually refresh the content suppose in case, automation failed. 1 hour refresh time will give you better results. Remember A. Wolff's comment about memory leaks.