通过这个code我可以显示动画GIF服务器脚本运行时:
With this code I can show an animated gif while the server script is running:
function calculateTotals() {
$('#results').load('getResults.php', null, showStatusFinished);
showLoadStatus();
}
function showLoadStatus() {
$('#status').html('');
}
function showStatusFinished() {
$('#status').html('Finished.');
}
不过,我想展示的远沿着剧本怎么样,比如状态加工生产线20000 342 ......,并有指望它,直到它完成。
However, I would like to display a status of how far along the script is, e.g. "Processing line 342 of 20000..." and have it count up until it is finished.
我怎么能这样做?我可以做一个服务器脚本包含不断更新的信息,但我在哪里把命令读取这个,比方说,每一秒?
How can I do that? I can make a server-script which constantly contains the updated information but where do I put the command to read this, say, every second?
阅读您的意见对安德鲁的回答后。
After reading your comments to Andrew's answer.
您会读的状态是这样的:
You would read the status like this:
function getStatus() {
$.getJSON("/status.php",{"session":0, "requestID":12345},
function(data) { //data is the returned JSON object from the server {name:"value"}
setStatus(data.status);
window.setTimeout("getStatus()",intervalInMS)
});
}
使用这个方法,你可以在服务器上打开几个同时XHR请求。
Using this method you can open several simultaneous XHR request on the server.
所有status.php以输出为:
all your status.php as to output is :
{"status":"We are done row 1040/45983459"}
不过,您可以输出尽可能多的信息,你想在响应和相应的处理(进料例如一个进度条或执行的动画..)
You can however output as many information you want in the response and to process it accordingly (feeding a progress bar for example or performing an animation..)
有关$ .getJSON的详细信息,请参阅 http://docs.jquery.com/Ajax/ jQuery.getJSON
For more information on $.getJSON see http://docs.jquery.com/Ajax/jQuery.getJSON