简体   繁体   中英

PHP or JavaScript periodic function call?

Is there any way to periodically check a database in PHP or JavaScript?

I want to perform a database query periodically in a single page. How can I do that?

You can use nnevala's solution , but it may cause problems if the request and your script takes longer than five seconds to execute.

It may be better to use a setTimeout recursive call:

function poll() {
    $.get('http://url/script', null, pollHandler);
}

function pollHandler(data) {
    console.log('Server said: ' + data);

    setTimeout(poll, 5000);
}

(The functions are split to prevent a memory leak.)

将JavaScript与setInterval函数一起使用,以调用向PHP脚本发出Ajax请求的函数,该脚本将处理所有数据库逻辑。

What GSto said. Quick example using jquery :

setInterval(function() {
  $.get('http://url/script', null, function(data) {
    console.log('Server said: ' + data);
  }
}, 5000); // every 5 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM