2014-01-22 36 views

回答

2

首先寫你在這種情況下,一個事件綁定到你的按鈕,將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(); 
}); 
+0

如果重新啓動需要超過10秒鐘,該怎麼辦? –

+0

讓我改變我的awnser,我誤解你的需要 –

+0

ps:不要忘記仍然綁定您的按鈕與preventDefault無論如何。 –

0

注:我並不確切地知道你的意思「重載服務器」什麼,如果實際重新加載頁面,任何你指定的呼叫(重新加載頁面)後,將不會被執行。

如果不重新加載頁面:

您可以使用jQuery delay功能,輸出的東西有延遲,但它主要是用於效果。

我通常使用的是javascript timeout,當它更關於文本等。

+0

如果您有重新加載服務器並開始你的網頁再次你可以傳遞一個消息,說「服務器重新加載」,實現取決於你如何做服務器重裝你的後端代碼的功能。 – dansv130

+0

嗨。該按鈕正在調用重新加載Apache服務器的腳本。 – JeffVader

+0

腳本在哪裏? Javascript或某種服務器端腳本? – dansv130

0

我會用僞代碼,但是這可以很容易地在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" 
相關問題