我有一个简单的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无论如何。 –