I have two asp.net pages and navigating from Page1 to Page2 takes some times. (Pa开发者_如何转开发ge2 does data access stuff). So, I want to show a loading page or indicator while Page2 finishes loading.
Is there any ways I could do this in jQuery?
Thanks.
One thing that you could do is call a page that performs the data access and puts it into memory (for example, using Memcached).
If it's not a hugely long process (it won't timeout), then you could make the call to your server using getJSON. This script can then access your database and put the result in cache. When finished with the processing, it will output a JSON response with anything that's needed to recall it (i.e. the key that you used to store it in memcached). When the response comes back, redirect to the page (passing the key), and read the required data for the page from cache.
Not sure what code you're doing your server stuff in, so I'll just give an example of the sort of thing I'd do in ASP .NET MVC.
JavaScript (on the click event or something):
$("#loadingIndicator").show();
$.getJSON('/DataAccess', function(data) {
window.location.href = '/CacheReader/' + data.Key;
});
Server:
// Returns the JSON with the key
public ActionResult DataAccess() {
var row = GetRow();
var key = Guid.NewGuid();
Cache.Store(key, row);
return new Json(new { Key = key });
}
// Using the key, get the data from cache and output nicely.
public ActionResult CacheReader(string key) {
var cachedRow = Cache.Read(key);
return new View(cachedRow);
}
The server code probably has some errors but it hopefully gets across my point. The code will show a loading indicator on the page until the JSON responds (which means everything is in cache). Then it will redirect to the correct page. It might be a good idea to have something that causes the indicator to go away and show a 'Oops' message on time out/error. Just so the user doesn't wait forever.
精彩评论