UTILS_FUNCTION Library

Eine nützliche Bibliothek für verschiedene Funktionen

Fra og med 01.03.2025. Se den nyeste version.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://updategreasyfork.deno.dev/scripts/528459/1545256/UTILS_FUNCTION%20Library.js

// ==UserScript==
// @name         UTILS_FUNCTION Library
// @namespace    dannysaurus.epik
// @version      1.0
// @description  Eine nützliche Bibliothek für verschiedene Funktionen
//
// @license      MIT
// ==/UserScript==

/* jslint esversion: 11 */
/* global unsafeWindow */
(() => {
    'use strict';
    unsafeWindow.dannysaurus_epik ||= {};
    unsafeWindow.dannysaurus_epik.libraries ||= {};

    unsafeWindow.dannysaurus_epik.libraries.UTILS_FUNCTION = (() => {

        /**
      * Throttle function calls with a timeOut between calls.
      * 
      * The timeout is not reset if the function is called again before the timeout has expired.
      * 
      * @param {Function} func - The function to throttle.
      * @param {number} waitMs - The time to wait between function calls in milliseconds.
      * @returns {Function} The throttled function.
      */
        throttle = (func, waitMs) => {
            let timeout = null;
            let argumnentsForNextCall = null;

            // Funktion, die später aufgerufen wird
            const runAfterTimeout = () => {
                if (argumnentsForNextCall) {
                    func.apply(null, argumnentsForNextCall);
                    argumnentsForNextCall = null;

                    timeout = setTimeout(runAfterTimeout, waitMs);
                } else {
                    timeout = null;
                }
            };

            return (...args) => {
                if (!timeout) {
                    func.apply(null, args);

                    timeout = setTimeout(runAfterTimeout, waitMs);
                } else {
                    argumnentsForNextCall = args;
                }
            };
        };

        return {
            throttle,
        };
    })();
})();