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;
};
/*@
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
This can be used as a default iterator
@ -157,49 +188,18 @@ Ox.throttle = function(fn, ms) {
var args,
timeout;
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() {
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);
fn.apply(null, args);
args = null;
timeout = setTimeout(function() {
if (args !== null) {
fn.apply(null, args);
}
timeout = null;
}, ms);
};
};
};