Reputation: 324
I have image gallery. and gallery has thumbnails when user click on thumbnail ajax request will fire. i want that when user click again on thumbnail request should not fire and use existing data.
$.getJSON(url, function(data) {
}
i want a js object and keep the json data for every image in that object when click is done on thumbnail, first check if have already loaded the data if yes, then don't make a ajax call and simply use the data that i fetched earlier should i hold data on objects ?? if yes! then how can i do this ?
Upvotes: 0
Views: 132
Reputation: 95066
Bind the initial click event with .one
, then within it bind a new click event after requesting the ajax.
$(".thumbnail").one("click",function(){
var request = $.getJSON(url);
$(this).on("click",function(){
request.done(function(data){
// do something with data when it is available
});
}).triggerHandler("click");
});
The first click on each thumbnail will request data then use the data, each subsequent click will just use the data
Upvotes: 2