![]() |
Smooth async requests
When requesting a sequence of data asynchronously using ajax the data is not returned at regular intervals, or necessarily in the order the requests were made. The following code uses setInterval to update the page at regular intervals - creating a smooth page-update. The data is inserted into table cells according to the order the data was received.
The setIntervals are not initiated until half the data has been received, and will be cleared when all data has been processed. (An else clause could be added so that the setInterval is cleared if there is an error in retrieving the remaining data.) asyncAjax.html Code:
<!DOCTYPE html>PHP Code:
|
I am very happy with the algorithm :thumbsup: and the principles involved but I might look to improve it a little:
The worker-function should be passed the 2 shifted values, not the array; I think this (in this.tbl) is the window object - I'll have to check this - so I'm creating an unnecessary global; The limit of 10 is defined twice - it should only be defined once, as a variable. However, as it stands, there can be a different number of requests and needed returns.. I should write the code to handle a failed request. It should clear the timer-interval, but should it also cancel any future requests? Maybe I can supply a true/false argument to determine whether future requests should be cancelled. I'll report back, but welcome any input :thumbsup: |
The following version handles errors in the requests, and it forces 404 failures on every other request to demonstrate.
Code:
<!DOCTYPE html> |
I've got this sussed now :cool:, based on a MultiAjax object and this code fragment:
Code:
var thisRun = new MultiAjax({Success is the callback to execute to update the DOM, receiving an index number (the requests' index) and the value returned. Failure is an alternative callback to handle failed requests (defaulting to the Success function). A random number is appended to the request-page so that the same value is not returned each time. Code:
<!DOCTYPE html> |
Well, I might as well include the version that can be supplied an array of pages like this:
Code:
var thisRun = new MultiAjax({Code:
<!DOCTYPE html> |
Whoops! I had to correct it to cancel the setInterval once all the requests have been processed. Last change, I promise :thumbsup:
Code:
<!DOCTYPE html> |
here are a few suggestions. ok, several, sorry; i got carried away...
Code:
<!DOCTYPE html>
2do -Capitals imply Constructors, properties and methods should be .camelCase'd() instead |
@rnd me Thank you very much :thumbsup:; I shall study!
Quote:
Quote:
Quote:
I like the idea of separating out the Base, and coercing a single page to an array is a good idea. Cheers again. Andy. |
Mmm, if there is only a single page will it request it multiple times? I'll have to check :thumbsup:
Edited: Ignore this. I see that you are appending the random numbers to the page(s), which will be required anyway for a single page. |
The timer-interval is no longer being cleared, because that.Limit is no longer defined(?).
If I can get this working then I could also disable the progress element in some way(?). Added: ignore me again please: Code:
MultiAjax.prototype = { |
..and to signify the progress is complete:
Code:
} else if (that.currentOne >= that.Limit) {Andy. |
| All times are GMT +1. The time now is 02:04 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.