Greasy Fork is available in English.

UTILS_FUNCTION Library

Eine nützliche Bibliothek für verschiedene Funktionen

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greatest.deepsurf.us/scripts/528459/1545384/UTILS_FUNCTION%20Library.js

  1. // ==UserScript==
  2. // @name UTILS_FUNCTION Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.0
  5. // @description library to modify functions and arrow functions
  6. //
  7. // @license MIT
  8. // ==/UserScript==
  9.  
  10. /* jslint esversion: 11 */
  11. /* global unsafeWindow */
  12. (() => {
  13. 'use strict';
  14. unsafeWindow.dannysaurus_epik ||= {};
  15. unsafeWindow.dannysaurus_epik.libraries ||= {};
  16.  
  17. unsafeWindow.dannysaurus_epik.libraries.UTILS_FUNCTION = (() => {
  18.  
  19. /**
  20. * Throttle function calls with a timeOut between calls.
  21. *
  22. * The timeout is not reset if the function is called again before the timeout has expired.
  23. *
  24. * @param {Function} func - The function to throttle.
  25. * @param {number} waitMs - The time to wait between function calls in milliseconds.
  26. * @returns {Function} The throttled function.
  27. */
  28. const throttle = (func, waitMs) => {
  29. let timeout = null;
  30. let argumnentsForNextCall = null;
  31.  
  32. // Funktion, die später aufgerufen wird
  33. const runAfterTimeout = () => {
  34. if (argumnentsForNextCall) {
  35. func.apply(null, argumnentsForNextCall);
  36. argumnentsForNextCall = null;
  37.  
  38. timeout = setTimeout(runAfterTimeout, waitMs);
  39. } else {
  40. timeout = null;
  41. }
  42. };
  43.  
  44. return (...args) => {
  45. if (!timeout) {
  46. func.apply(null, args);
  47.  
  48. timeout = setTimeout(runAfterTimeout, waitMs);
  49. } else {
  50. argumnentsForNextCall = args;
  51. }
  52. };
  53. };
  54.  
  55. return {
  56. throttle,
  57. };
  58. })();
  59. })();