Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: util.throttle misbehavior #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/DataTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

/*jslint evil: true, undef: true, browser: true */
/*globals $,require,jQuery,define,_selector_run,_selector_opts,_selector_first,_selector_row_indexes,_ext,_Api,_api_register,_api_registerPlural,_re_new_lines,_re_html,_re_formatted_numeric,_re_escape_regex,_empty,_intVal,_numToDecimal,_isNumber,_isHtml,_htmlNumeric,_pluck,_pluck_order,_range,_stripHtml,_unique,_fnBuildAjax,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAjaxDataSrc,_fnAddColumn,_fnColumnOptions,_fnAdjustColumnSizing,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnVisbleColumns,_fnGetColumns,_fnColumnTypes,_fnApplyColumnDefs,_fnHungarianMap,_fnCamelToHungarian,_fnLanguageCompat,_fnBrowserDetect,_fnAddData,_fnAddTr,_fnNodeToDataIndex,_fnNodeToColumnIndex,_fnGetCellData,_fnSetCellData,_fnSplitObjNotation,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnGetDataMaster,_fnClearTable,_fnDeleteIndex,_fnInvalidate,_fnGetRowElements,_fnCreateTr,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAddOptionsHtml,_fnDetectHeader,_fnGetUniqueThs,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnFilterCreateSearch,_fnEscapeRegex,_fnFilterData,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnInfoMacros,_fnInitialise,_fnInitComplete,_fnLengthChange,_fnFeatureHtmlLength,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnFeatureHtmlTable,_fnScrollDraw,_fnApplyToChildren,_fnCalculateColumnWidths,_fnThrottle,_fnConvertToWidth,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnSortFlatten,_fnSort,_fnSortAria,_fnSortListener,_fnSortAttachListener,_fnSortingClasses,_fnSortData,_fnSaveState,_fnLoadState,_fnSettingsFromNode,_fnLog,_fnMap,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnLengthOverflow,_fnRenderer,_fnDataSource,_fnRowAttributes*/
/*globals $,require,jQuery,define,_selector_run,_selector_opts,_selector_first,_selector_row_indexes,_ext,_Api,_api_register,_api_registerPlural,_re_new_lines,_re_html,_re_formatted_numeric,_re_escape_regex,_empty,_intVal,_numToDecimal,_isNumber,_isHtml,_htmlNumeric,_pluck,_pluck_order,_range,_stripHtml,_unique,_fnBuildAjax,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAjaxDataSrc,_fnAddColumn,_fnColumnOptions,_fnAdjustColumnSizing,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnVisbleColumns,_fnGetColumns,_fnColumnTypes,_fnApplyColumnDefs,_fnHungarianMap,_fnCamelToHungarian,_fnLanguageCompat,_fnBrowserDetect,_fnAddData,_fnAddTr,_fnNodeToDataIndex,_fnNodeToColumnIndex,_fnGetCellData,_fnSetCellData,_fnSplitObjNotation,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnGetDataMaster,_fnClearTable,_fnDeleteIndex,_fnInvalidate,_fnGetRowElements,_fnCreateTr,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAddOptionsHtml,_fnDetectHeader,_fnGetUniqueThs,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnFilterCreateSearch,_fnEscapeRegex,_fnFilterData,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnInfoMacros,_fnInitialise,_fnInitComplete,_fnLengthChange,_fnFeatureHtmlLength,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnFeatureHtmlTable,_fnScrollDraw,_fnApplyToChildren,_fnCalculateColumnWidths,_fnThrottle,_fnDebounce,_fnConvertToWidth,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnSortFlatten,_fnSort,_fnSortAria,_fnSortListener,_fnSortAttachListener,_fnSortingClasses,_fnSortData,_fnSaveState,_fnLoadState,_fnSettingsFromNode,_fnLog,_fnMap,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnLengthOverflow,_fnRenderer,_fnDataSource,_fnRowAttributes*/

(function( factory ) {
"use strict";
Expand Down
1 change: 1 addition & 0 deletions js/api/api.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ $.extend( DataTable.ext.internal, {
_fnApplyToChildren: _fnApplyToChildren,
_fnCalculateColumnWidths: _fnCalculateColumnWidths,
_fnThrottle: _fnThrottle,
_fnDebounce: _fnDebounce,
_fnConvertToWidth: _fnConvertToWidth,
_fnGetWidestNode: _fnGetWidestNode,
_fnGetMaxLenString: _fnGetMaxLenString,
Expand Down
64 changes: 46 additions & 18 deletions js/api/api.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,60 @@ DataTable.util = {
*/
throttle: function ( fn, freq ) {
var
frequency = freq !== undefined ? freq : 200,
last,
call,
that,
timer;

freq = freq !== undefined ? freq : 200;

function timeout() {
fn.apply( that, call );
call = undefined;
that = undefined;
timer = setTimeout( function () {
timer = undefined;
if ( call ) {
timeout();
}
}, freq );
}

return function () {
var
that = this,
now = +new Date(),
args = arguments;

if ( last && now < last + frequency ) {
clearTimeout( timer );

timer = setTimeout( function () {
last = undefined;
fn.apply( that, args );
}, frequency );
}
else {
last = now;
fn.apply( that, args );
that = this;
call = arguments;
if ( !timer ) {
timeout();
}
};
},


/**
* Debounce the calls to a function. Arguments and context are maintained
* for the debounced function.
*
* @param {function} fn Function to be called
* @param {integer} wait Wait after last call in mS
* @return {function} Wrapped function
*/
debounce: function ( fn, wait ) {
var timer;

wait = wait !== undefined ? wait : 200;

return function () {
var args = arguments;
var that = this;
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout( function () {
timer = undefined;
fn.apply( that, args );
}, wait );
};
},

/**
* Escape a string such that it can be used in a regular expression
*
Expand Down
2 changes: 1 addition & 1 deletion js/core/core.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function _fnFeatureHtmlFilter ( settings )
.on(
'keyup.DT search.DT input.DT paste.DT cut.DT',
searchDelay ?
_fnThrottle( searchFn, searchDelay ) :
_fnDebounce( searchFn, searchDelay ) :
searchFn
)
.on( 'keypress.DT', function(e) {
Expand Down
9 changes: 9 additions & 0 deletions js/core/core.sizing.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ function _fnCalculateColumnWidths ( oSettings )
*/
var _fnThrottle = DataTable.util.throttle;

/**
* Debounce the calls to a function. Arguments and context are maintained for
* the debounced function
* @param {function} fn Function to be called
* @param {int} [wait=200] Wait after last call in mS
* @returns {function} wrapped function
* @memberof DataTable#oApi
*/
var _fnDebounce = DataTable.util.debounce;

/**
* Convert a CSS unit width to pixels (e.g. 2em)
Expand Down