Skip to content
Browse files

Prevent multiple request for the same image

  • Loading branch information...
1 parent 251094c commit 13a28583bde3002fb2c8c8135964ef4735058bd2 Dumitru committed
Showing with 10 additions and 5 deletions.
  1. +8 −5 assets/jquery.imageCache.js
  2. +2 −0 demo.html
View
13 assets/jquery.imageCache.js
@@ -32,13 +32,16 @@
var src = $(img).attr('src') || $(img).attr('data-src');
if (localStorage) {
var localSrc = localStorage[src];
- if (localSrc) {
+ if (localSrc != null && localSrc != 'false') {
$(img).attr('src', localSrc);
} else {
- $.get(self.config.base64ImageEncoderPath + src, function (data) {
- localStorage[src] = data;
- $(img).attr('src', data);
- });
+ $(img).attr('src', src);
+ if (localStorage[src] === null) {
+ localStorage[src] = 'false';
+ $.get(self.config.base64ImageEncoderPath + src, function (data) {
+ localStorage[src] = data;
+ });
+ }
}
}
});
View
2 demo.html
@@ -24,6 +24,8 @@
<ul>
<li><img src="" data-src="assets/test-image-4.jpg" class="imageCache" /></li>
<li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
+ <li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
+ <li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
</ul>
<script type="text/javascript">

0 comments on commit 13a2858

Please sign in to comment.
Something went wrong with that request. Please try again.