Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /*
  2. Many beginning jQuery people forget to save the results of a selection and
  3. just call $('foo') over and over. The only time you would need to do that is if the
  4. DOM changes. Otherwise you can just save the results somewhere when the
  5. selection is first made. Much more performant.
  6.  
  7. Here's a mini jQuery plugin that just caches the results of a selector after
  8. the first time it's invoked.
  9. */
  10.  
  11. (function($){
  12. $.stale = function(selector){
  13. return $.lazy.lookup[selector] || ($.lazy.lookup[selector] = $(selector));
  14. };
  15. $.fresh = function(selector){
  16. return ($.lazy.lookup[selector] = $(selector));
  17. }
  18. $.stale.lookup = {};
  19. })(jQuery);
  20.  
  21. //Usage
  22.  
  23. $.stale('#button').click(function(){
  24. $.stale('#button').val();
  25. });
  26.  
  27. //if for some reason you need to refresh the lookup:
  28. $.fresh('#button');
Add Comment
Please, Sign In to add comment