1
Votes

Caching AJAX Results in JavaScript

Posted By tamilsolai on May 31, 2010   FROM: advancedphptutorial.blogspot.com report abuse

Caching Ajax Results, jquery cache ajax, ajax cache control, internet explorer caching ajax

AJAX is an awesome tool. AJAX requests are usually faster than regular page loads and allow for a wealth of dynamism within a page. Unfortunately many people do not properly cache request information when they can. Let me show you how I cache AJAX requests — it’s super easy!

My example will use my TwitterGitter plugin to grab a user’s tweets. Once we have the user’s tweet information, we pull it from cache instead of making a duplicate AJAX request.
//our cache object
var cache = {};
var formatTweets(info) {
//formats tweets, does whatever you want with the tweet information
};

//event
$('myForm').addEvent('submit',function() {
var handle = $('handle').value; //davidwalshblog, for example
var cacheHandle = handle.toLowerCase();
if(cache[cacheHandle]) {
formatTweets(cache[cacheHandle]);
}
else {
//gitter
var myTwitterGitter = new TwitterGitter(handle,{
count: 10,
onComplete: function(tweets,user) {
cache[cacheHandle] = tweets;
formatTweets(tweets);
}
}).retrieve();
}
});

Read Full Story from advancedphptutorial.blogspot.com

Post new comment

  • Allowed HTML tags: <a> <strong> <ul> <ol> <li> <table> <tr> <td> <tbody> <embed> <object> <param> <b> <p> <i> <div> <h3> <h4> <br> <img> <style>
  • Lines and paragraphs break automatically.

More information about formatting options