From 2077ea718a28dc768ea26024bf2a5c446e9111b0 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 2 Feb 2014 02:19:19 +0530 Subject: [PATCH] add Ox.once, fix docs --- source/Ox/js/Function.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/source/Ox/js/Function.js b/source/Ox/js/Function.js index bbcdf9aa..ee6476e0 100644 --- a/source/Ox/js/Function.js +++ b/source/Ox/js/Function.js @@ -59,8 +59,8 @@ Ox.cache = function(fn, options) { }; /*@ -Ox.debounce Runs a function once it stops being called for a given time frame - (fn[, ms][, immediate]) -> Throttled function +Ox.debounce Runs a function once it stops being called for a given interval + (fn[, ms][, immediate]) -> Debounced function fn Function to debounce ms Interval in milliseconds immediate If true, function is called once immediately @@ -113,6 +113,21 @@ Ox.noop = function() { Ox.isFunction(callback) && callback(); }; +/*@ +Ox.once Runs a function once, and then never again + (fn[, ms]) -> Function that will run only once + fn Function to run once +@*/ +Ox.once = function(fn) { + var once = false; + return function() { + if (!once) { + once = true; + fn.apply(null, args); + } + }; +}; + /*@ Ox.queue Queue of asynchronous function calls with cached results The results are cached based on all arguments to `fn`, except the last one, @@ -179,7 +194,7 @@ Ox.queue = function(fn, maxThreads) { }; /*@ -Ox.throttle Runs a function at most once per given time frame +Ox.throttle Runs a function at most once per given interval (fn[, ms]) -> Throttled function fn Function to throttle ms Interval in milliseconds