[英]PHP or JavaScript periodic function call?
Is there any way to periodically check a database in PHP or JavaScript? 有什么方法可以定期检查PHP或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. 您可以使用nnevala的解决方案 ,但是如果请求和脚本执行的时间超过五秒钟,则可能会导致问题。
It may be better to use a setTimeout
recursive call: 最好使用
setTimeout
递归调用:
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请求的函数,该脚本将处理所有数据库逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.