2014-01-22 34 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" 
相关问题