I need a suggestion about jQuery ajax
Code:
$(element).click(function() {
jQuery.ajax(
type: 'GET',
url: 'file.php?id=xxx',
async: false,
success: function(data) {
...
}
);
});
file.php do a select on database and it will sh开发者_运维百科ow about 500.000 lines.
I need to do a progressbar(0 - 100%) while ajax get these lines.
does anyone have any suggestions?
maybe should I use an iframe
to do it? I don't know if it's possible to do with ajax.
You should send more ajax requests to be able to show progress, it isn't possible with single one
On your server, you'll need to implement a function that takes a given id (and user id or user ip address), and returns the current status of the given request. This is entirely dependent on how you do your SQL or other server-side activity, but it's a core concept in understanding "what percent is done on my activity?".
That said, let's say to implement "file.php?request=status&id=..." which returns a JSON struct { :status => "0.50" } to imply 50% completed on the task. Sorry again that I cannot help with this portion, but it's up to your specific code on the server as how best to implement this.
Once you kick off your AJAX (as above), set a time to occasionally ping your server with updates.
$('#progressBar').progressBar( 'value', 0 );
var updateProgressBar = function() {
$.ajax({
'type': get,
'url': 'file.php?request=status&id=...',
'success': function(json) { $('#progressBar').progressBar( 'value', json.status * 100 ); }
};
var updater = setTimeout(updateProgressBar, 2000); //make sure to clear this when request completes
Hopefully this will set you on the way to implementing a proper progress bar and let me know if you have any questions with the implementation or ideas.
精彩评论