我有一個簡單的PHP頁面,它有一個按鈕。點擊按鈕後,Apache服務器將重新加載。這是打算和按預期工作。Jquery - 在Apache服務器重新加載的同時在客戶端上運行?
當按鈕被按下時,有什麼辦法可以等待10秒鐘,然後顯示'Apache Reloaded'?或者因爲服務器已被重新加載會殺死客戶正在做什麼?
感謝
我有一個簡單的PHP頁面,它有一個按鈕。點擊按鈕後,Apache服務器將重新加載。這是打算和按預期工作。Jquery - 在Apache服務器重新加載的同時在客戶端上運行?
當按鈕被按下時,有什麼辦法可以等待10秒鐘,然後顯示'Apache Reloaded'?或者因爲服務器已被重新加載會殺死客戶正在做什麼?
感謝
首先寫你在這種情況下,一個事件綁定到你的按鈕,將ID#鍵
然後你調用檢查對象的方法,ajax_check.reload_apache();
這將啓動ajax方法,將請求發送到您的網址(這將重新啓動apache),成功後它將激發Bridge方法,它將循環ajax請求(到不同的url,因爲我們不'我想每5秒鐘重新加載一次apache,最多5次嘗試。 (你可以在你的構造函數中改變這個數字)。
ps:你需要jquery,當然它仍然需要一點驗證,但它應該適合你的需求。
function check() {
this.max_errors = 5; // maximum of tries
this.timeout = 5000; // 5 seconds delay in every request
this.url = 'test22'; // url to reload apache (script url w/e)
this.url_check = 'index'; // any url from your webserver, so you can check if its up
this.status = 0; // don't change
this.interval = 0; // don't change
this.errors = 0; // don't change
this.flag_first = 0; // don't change
}
check.prototype = {
constructor: check,
ajax: function() {
var self = this;
$.ajax({
// if first call use self.url to reload apache
// if requesting for server alive use the url_check
url: (self.flag_first == 1) ? self.url_check : self.url
}).done(function() {
if(self.flag_first == 1){
self.status = 1;
window.clearInterval(self.interval);
console.log('Apache Reloaded');
}else{
// RUNS for the first time
self.flag_first = 1;
self.bridge();
}
}).fail(function(){
if(self.flag_first == 1){
self.errors++;
if(self.errors >= self.max_errors){ // if errors are higher or equal to max_errors
alert('Stopped');
window.clearInterval(self.interval);
self.errors = 0; // reset errors
}
}
});
},
bridge: function() {
var self = this;
this.interval = window.setInterval(function() {
self.ajax();
}, self.timeout);
},
reload_apache: function(){
this.ajax();
}
};
var ajax_check = new check();
$('#button').click(function(e){
e.preventDefault();
ajax_check.reload_apache();
});
注:我並不確切地知道你的意思「重載服務器」什麼,如果實際重新加載頁面,任何你指定的呼叫(重新加載頁面)後,將不會被執行。
如果不重新加載頁面:
您可以使用jQuery delay功能,輸出的東西有延遲,但它主要是用於效果。
我通常使用的是javascript timeout,當它更關於文本等。
我會用僞代碼,但是這可以很容易地在JavaScript或jQuery的
On button click:
Send restart request with ajax
Show "Server is restarting" in an overlay for example
Do:
- wait 2 seconds
While (dummy ajax request fails)
Replace "Server is restarting" with "Server restart complete"
如果重新啓動需要超過10秒鐘,該怎麼辦? –
讓我改變我的awnser,我誤解你的需要 –
ps:不要忘記仍然綁定您的按鈕與preventDefault無論如何。 –