ui.util.rateLimit
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
עוטף פונקציה כדי לאפשר לקרוא לה, לכל היותר, פעם אחת בכל מרווח. אם קוראים לפונקציית העטיפה יותר מפעם אחת, רק הקריאה הראשונה תעבור, ולא תהיה השפעה לקריאות הבאות עד שיחלוף מרווח הזמן. אפשר להשתמש בזה כדי לוודא שפונקציה שדורשת הרבה משאבים תופעל באופן מיידי, אבל לא תופעל שוב ושוב.
דוגמה לשימוש: כדי שהקריאה החוזרת (callback) ללחיצה על ui.Button לא תפעל פעמיים בטעות בגלל לחיצה כפולה על הלחצן.
הפונקציה מחזירה את הפונקציה עם הגבלת הקצב.
שימוש | החזרות |
---|
ui.util.rateLimit(func, delay, scope) | פונקציה |
ארגומנט | סוג | פרטים |
---|
func | פונקציה | הפונקציה שרוצים להפעיל. |
delay | מספר | אחרי שהפונקציה מופעלת ומוצאת לפועל, מספר אלפיות השנייה שצריך להמתין לפני שמאפשרים הפעלה נוספת של הפונקציה. |
scope | אובייקט, אופציונלי | אובייקט שבהיקף שלו רוצים להפעיל את הפונקציה. |
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[null,null,["עדכון אחרון: 2025-07-26 (שעון UTC)."],[[["\u003cp\u003eLimits a function's execution to at most once per specified interval, preventing rapid, repeated calls.\u003c/p\u003e\n"],["\u003cp\u003eUseful for optimizing expensive functions or preventing unintended double-click actions, such as on UI buttons.\u003c/p\u003e\n"],["\u003cp\u003eReturns a rate-limited version of the original function, accepting the function, delay, and optional scope as parameters.\u003c/p\u003e\n"],["\u003cp\u003eUtilizes a delay mechanism to ensure subsequent calls are ignored until the interval has elapsed, guaranteeing controlled execution.\u003c/p\u003e\n"]]],[],null,["# ui.util.rateLimit\n\n\u003cbr /\u003e\n\nWraps a function to allow it to be called, at most, once per interval. If the wrapper function is called more than once, only the first call will go through, and no subsequent invocations will have an effect until the interval has elapsed. This can be used to ensure a function that is expensive to run executes immediately but doesn't execute repeatedly.\n\n\u003cbr /\u003e\n\nExample use: For the callback to a click on a ui.Button, in order to prevent the button from being accidentally double-clicked and the callback running twice.\n\nReturns the rate-limited function.\n\n| Usage | Returns |\n|---------------------------------------------|----------|\n| `ui.util.rateLimit(func, delay, `*scope*`)` | Function |\n\n| Argument | Type | Details |\n|----------|------------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | Function | Function to call. |\n| `delay` | Number | After the function is called and executed, the number of milliseconds to delay before allowing an additional invocation of the function. |\n| `scope` | Object, optional | Object in whose scope to call the function. |"]]