cosmetic changes

This commit is contained in:
rolux 2014-02-02 02:05:48 +05:30
parent cb6644e08d
commit 5a615730e9

View file

@ -58,6 +58,37 @@ Ox.cache = function(fn, options) {
return ret; return ret;
}; };
/*@
Ox.debounce <f> Runs a function once it stops being called for a given time frame
(fn[, ms][, immediate]) -> <f> Throttled function
fn <f> Function to debounce
ms <n|250> Interval in milliseconds
immediate <b|false> If true, function is called once immediately
@*/
Ox.debounce = function(fn/*, ms, immediate*/) {
var args,
immediate = Ox.last(arguments) === true,
ms = Ox.isNumber(arguments[1]) ? arguments[1] : 250,
timeout;
return function() {
args = arguments;
if (!timeout) {
if (immediate) {
fn.apply(null, args);
args = null;
}
} else {
clearTimeout(timeout);
}
timeout = setTimeout(function() {
if (args !== null) {
fn.apply(null, args);
}
timeout = null;
}, ms);
};
};
/*@ /*@
Ox.identity <f> Returns its first argument Ox.identity <f> Returns its first argument
This can be used as a default iterator This can be used as a default iterator
@ -157,49 +188,18 @@ Ox.throttle = function(fn, ms) {
var args, var args,
timeout; timeout;
ms = arguments.length == 1 ? 250 : ms; ms = arguments.length == 1 ? 250 : ms;
return function() {
args = arguments;
if (!timeout) {
fn.apply(null, args);
args = null;
timeout = setTimeout(function() {
if (args !== null) {
fn.apply(null, args);
}
timeout = null;
}, ms);
};
};
};
/*@
Ox.debounce <f> Runs a function once it stops being called for a given time frame
(fn[, ms][, immediate]) -> <f> Throttled function
fn <f> Function to debounce
ms <n|250> Interval in milliseconds
immediate <b|false> If true, function is called once immediately
@*/
Ox.debounce = function(fn/*, ms, immediate*/) {
var args,
immediate = Ox.last(arguments) === true,
ms = Ox.isNumber(arguments[1]) ? arguments[1] : 250,
timeout;
return function() { return function() {
args = arguments; args = arguments;
if (!timeout) { if (!timeout) {
if (immediate) { fn.apply(null, args);
fn.apply(null, args); args = null;
args = null; timeout = setTimeout(function() {
} if (args !== null) {
} else { fn.apply(null, args);
clearTimeout(timeout); }
} timeout = null;
timeout = setTimeout(function() { }, ms);
if (args !== null) { };
fn.apply(null, args);
}
timeout = null;
}, ms);
}; };
}; };