jquery.once.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * jQuery Once Plugin v1.2
  3. * http://plugins.jquery.com/project/once
  4. *
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. */
  9. (function ($) {
  10. var cache = {}, uuid = 0;
  11. /**
  12. * Filters elements by whether they have not yet been processed.
  13. *
  14. * @param id
  15. * (Optional) If this is a string, then it will be used as the CSS class
  16. * name that is applied to the elements for determining whether it has
  17. * already been processed. The elements will get a class in the form of
  18. * "id-processed".
  19. *
  20. * If the id parameter is a function, it will be passed off to the fn
  21. * parameter and the id will become a unique identifier, represented as a
  22. * number.
  23. *
  24. * When the id is neither a string or a function, it becomes a unique
  25. * identifier, depicted as a number. The element's class will then be
  26. * represented in the form of "jquery-once-#-processed".
  27. *
  28. * Take note that the id must be valid for usage as an element's class name.
  29. * @param fn
  30. * (Optional) If given, this function will be called for each element that
  31. * has not yet been processed. The function's return value follows the same
  32. * logic as $.each(). Returning true will continue to the next matched
  33. * element in the set, while returning false will entirely break the
  34. * iteration.
  35. */
  36. $.fn.once = function (id, fn) {
  37. if (typeof id != 'string') {
  38. // Generate a numeric ID if the id passed can't be used as a CSS class.
  39. if (!(id in cache)) {
  40. cache[id] = ++uuid;
  41. }
  42. // When the fn parameter is not passed, we interpret it from the id.
  43. if (!fn) {
  44. fn = id;
  45. }
  46. id = 'jquery-once-' + cache[id];
  47. }
  48. // Remove elements from the set that have already been processed.
  49. var name = id + '-processed';
  50. var elements = this.not('.' + name).addClass(name);
  51. return $.isFunction(fn) ? elements.each(fn) : elements;
  52. };
  53. /**
  54. * Filters elements that have been processed once already.
  55. *
  56. * @param id
  57. * A required string representing the name of the class which should be used
  58. * when filtering the elements. This only filters elements that have already
  59. * been processed by the once function. The id should be the same id that
  60. * was originally passed to the once() function.
  61. * @param fn
  62. * (Optional) If given, this function will be called for each element that
  63. * has not yet been processed. The function's return value follows the same
  64. * logic as $.each(). Returning true will continue to the next matched
  65. * element in the set, while returning false will entirely break the
  66. * iteration.
  67. */
  68. $.fn.removeOnce = function (id, fn) {
  69. var name = id + '-processed';
  70. var elements = this.filter('.' + name).removeClass(name);
  71. return $.isFunction(fn) ? elements.each(fn) : elements;
  72. };
  73. })(jQuery);