在jquery中每24小时调用一次ajax调用

时间:2015-05-22 08:25:33

标签: php jquery ajax

如何在每24小时后触发Jquery ajax调用?

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

2 个答案:

答案 0 :(得分:2)

如果您想坚持使用jQuery和PHP,您可以执行以下操作:

//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();

请记住添加一个按钮以手动刷新内容,以防万一,自动化失败。 1小时的刷新时间将为您带来更好的效果。记住A. Wolff关于内存泄漏的评论。

答案 1 :(得分:0)

如果您正在使用Android TV应用,您可能只想查看:

https://developer.android.com/reference/android/app/job/JobScheduler.html

在浏览器中设置JS计时器并持续运行您的应用程序24/7等待超时发生是不切实际的 相反,您可以找到一种解决方法,例如创建一个每24小时刷新一次内容的调度程序类并将其存储在本地,您的浏览器只需刷新本地数据库或文件中的内容。

相关问题