![](/img/trans.png)
[英]how to run php script automatically every minute without access to that page?
[英]how to run 'free -m' every minute & add to current page (php only)
我需要
因此,該腳本僅在從瀏覽器調用free.php的情況下運行。 我只想使用php 5.3 + javascript / AJAX + html (=無cron,jQuery等)
set_time_limit(0);
$results = array();
while (TRUE) {
system('free -m', $result);
$results[] = $result;
sleep(60);
}
但是,這種事情可以很好地解決。
我知道您不想在這里使用jQuery,但這只是一種可用的工具。 您可以將其完全替換為您喜歡的任何ajax庫。
關鍵是要使用setInterval
管理Javascript計時器 。
// Here's the function that will be run.
function get_free_mem() {
$.get('/free_mem.php', {}, function(data){
// data contains the HTML output from the script.
// '#somewhere' is the HTML element that you want the
// output of the script appended to.
$('#somewhere').append(data);
}, 'html');
}
// And here's how we'll run it.
var timer = setInterval(get_free_mem, 60 * 1000); // 60 seconds
這是free_mem.php
:
<pre><?php passthru('free -m'); ?></pre>
每60秒,將觸發get_free_mem
函數,該函數將請求PHP腳本並將輸出附加到頁面上的HTML元素。
(此代碼未經測試。)
(是的,我意識到setInterval
可能會適得其反,如果被調用的腳本需要一段時間才能返回,則更新時間可能會不均勻,而使用setTimeout
並在ajax完成時重新觸發它是更好的做法,但是我相信被調用的腳本非常簡單,在這里應該不會有太大問題。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.