Commit ce98259c authored by jiangjiantao's avatar jiangjiantao

防抖点击

parent 04d33f34
# Changelog
## [Unreleased][]
## [2.2.1][] - 2020-06-08
### Changed
- Upgrade package
- Add Browserstack testing
- Add more detailed usage example to README (#36)
- Use ES2015+ features
[unreleased]: https://github.com/niksy/throttle-debounce/compare/v2.2.1...HEAD
[2.2.1]: https://github.com/niksy/throttle-debounce/tree/v2.2.1
This diff is collapsed.
# throttle-debounce
[![Build Status][ci-img]][ci]
[![BrowserStack Status][browserstack-img]][browserstack]
[![Mentioned in Awesome Micro npm Packages][awesome-img]][awesome]
Throttle and debounce functions.
This module is the same as [jquery-throttle-debounce][jquery-throttle-debounce]
([with some differences](#differences-with-original-module)), but it’s
transferred to ES Modules and CommonJS format.
## Install
```sh
npm install throttle-debounce --save
```
## Usage
### `throttle`
```js
import { throttle } from 'throttle-debounce';
const throttleFunc = throttle(1000, false, (num) => {
console.log('num:', num);
});
// Can also be used like this, because noTrailing is false by default
const throttleFunc = throttle(1000, () => {
console.log('num:', num);
});
throttleFunc(1); // Will execute the callback
throttleFunc(2); // Won’t execute callback
throttleFunc(3); // Won’t execute callback
// Will execute the callback, because noTrailing is false,
// but if we set noTrailing to true, this callback won’t be executed
throttleFunc(4);
setTimeout(() => {
throttleFunc(10); // Will execute the callback
}, 1200);
// Output
// num: 1
// num: 4
// num: 10
```
### `debounce`
```js
import { debounce } from 'throttle-debounce';
const debounceFunc = debounce(1000, false, (num) => {
console.log('num:', num);
});
// Can also be used like this, because atBegin is false by default
const debounceFunc = debounce(1000, () => {
console.log('num:', num);
});
// Won’t execute the callback, because atBegin is false,
// but if we set atBegin to true, this callback will be executed.
debounceFunc(1);
debounceFunc(2); // Won’t execute callback
debounceFunc(3); // Won’t execute callback
// Will execute the callback,
// but if we set atBegin to true, this callback won’t be executed.
debounceFunc(4);
setTimeout(() => {
debounceFunc(10); // Will execute the callback
}, 1200);
// Output
// num: 4
// num: 10
```
### Cancelling
Debounce and throttle can both be cancelled by calling the `cancel` function.
```js
const throttleFunc = throttle(300, () => {
// Throttled function
});
throttleFunc.cancel();
const debounceFunc = debounce(300, () => {
// Debounced function
});
debounceFunc.cancel();
```
The logic that is being throttled or debounced will no longer be called.
## API
### throttle(delay, noTrailing, callback, debounceMode)
Returns: `Function`
Throttle execution of a function. Especially useful for rate limiting execution
of handlers on events like resize and scroll.
#### delay
Type: `Number`
A zero-or-greater delay in milliseconds. For event callbacks, values around 100
or 250 (or even higher) are most useful.
#### noTrailing
Type: `Boolean`
Optional, defaults to false. If noTrailing is true, callback will only execute
every `delay` milliseconds while the throttled-function is being called. If
noTrailing is false or unspecified, callback will be executed one final time
after the last throttled-function call. (After the throttled-function has not
been called for `delay` milliseconds, the internal counter is reset)
#### callback
Type: `Function`
A function to be executed after delay milliseconds. The `this` context and all
arguments are passed through, as-is, to `callback` when the throttled-function
is executed.
#### debounceMode
Type: `Boolean`
If `debounceMode` is true (at begin), schedule `clear` to execute after `delay`
ms. If `debounceMode` is false (at end), schedule `callback` to execute after
`delay` ms.
### debounce(delay, atBegin, callback)
Returns: `Function`
Debounce execution of a function. Debouncing, unlike throttling, guarantees that
a function is only executed a single time, either at the very beginning of a
series of calls, or at the very end.
#### delay
Type: `Number`
A zero-or-greater delay in milliseconds. For event callbacks, values around 100
or 250 (or even higher) are most useful.
#### atBegin
Type: `Boolean`
Optional, defaults to false. If `atBegin` is false or unspecified, callback will
only be executed `delay` milliseconds after the last debounced-function call. If
`atBegin` is true, callback will be executed only at the first
debounced-function call. (After the throttled-function has not been called for
`delay` milliseconds, the internal counter is reset).
#### callback
Type: `Function`
A function to be executed after delay milliseconds. The `this` context and all
arguments are passed through, as-is, to `callback` when the debounced-function
is executed.
## Differences with original module
- Dependancy on jQuery is removed, so if you rely on GUIDs set by jQuery, plan
accordingly
- There is no standalone version available, so don’t rely on `$.throttle` and
`$.debounce` to be available
## Browser support
Tested in IE9+ and all modern browsers.
## Test
For automated tests, run `npm run test:automated` (append `:watch` for watcher
support).
## License
<!-- prettier-ignore-start -->
**Original module license:** Copyright (c) 2010 "Cowboy" Ben Alman (Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/)
**This module license:** MIT © [Ivan Nikolić](http://ivannikolic.com)
[ci]: https://travis-ci.org/niksy/throttle-debounce
[ci-img]: https://travis-ci.org/niksy/throttle-debounce.svg?branch=master
[browserstack]: https://www.browserstack.com/
[browserstack-img]: https://www.browserstack.com/automate/badge.svg?badge_key=QWo5dGJqTzNkMWFnVXZqYzNvVDF1Q2p2Mm84Skc5ZTZFUUlnYkdEcFhQTT0tLTc1bVR3U284Uk9LZEFiSGR0NS8rY1E9PQ==--4a20321e30f225033ed6840c2fb71c6d1a8d2d1a
[awesome]: https://github.com/parro-it/awesome-micro-npm-packages
[awesome-img]: https://awesome.re/mentioned-badge.svg
[jquery-throttle-debounce]: https://github.com/cowboy/jquery-throttle-debounce
<!-- prettier-ignore-end -->
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/* eslint-disable no-undefined,no-param-reassign,no-shadow */
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
*
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
* throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
* after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
* the internal counter is reset).
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the throttled-function is executed.
* @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
* schedule `callback` to execute after `delay` ms.
*
* @returns {Function} A new, throttled, function.
*/
function throttle (delay, noTrailing, callback, debounceMode) {
/*
* After wrapper has stopped being called, this timeout ensures that
* `callback` is executed at the proper times in `throttle` and `end`
* debounce modes.
*/
var timeoutID;
var cancelled = false; // Keep track of the last time `callback` was executed.
var lastExec = 0; // Function to clear existing timeout
function clearExistingTimeout() {
if (timeoutID) {
clearTimeout(timeoutID);
}
} // Function to cancel next exec
function cancel() {
clearExistingTimeout();
cancelled = true;
} // `noTrailing` defaults to falsy.
if (typeof noTrailing !== 'boolean') {
debounceMode = callback;
callback = noTrailing;
noTrailing = undefined;
}
/*
* The `wrapper` function encapsulates all of the throttling / debouncing
* functionality and when executed will limit the rate at which `callback`
* is executed.
*/
function wrapper() {
for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
arguments_[_key] = arguments[_key];
}
var self = this;
var elapsed = Date.now() - lastExec;
if (cancelled) {
return;
} // Execute `callback` and update the `lastExec` timestamp.
function exec() {
lastExec = Date.now();
callback.apply(self, arguments_);
}
/*
* If `debounceMode` is true (at begin) this is used to clear the flag
* to allow future `callback` executions.
*/
function clear() {
timeoutID = undefined;
}
if (debounceMode && !timeoutID) {
/*
* Since `wrapper` is being called for the first time and
* `debounceMode` is true (at begin), execute `callback`.
*/
exec();
}
clearExistingTimeout();
if (debounceMode === undefined && elapsed > delay) {
/*
* In throttle mode, if `delay` time has been exceeded, execute
* `callback`.
*/
exec();
} else if (noTrailing !== true) {
/*
* In trailing throttle mode, since `delay` time has not been
* exceeded, schedule `callback` to execute `delay` ms after most
* recent execution.
*
* If `debounceMode` is true (at begin), schedule `clear` to execute
* after `delay` ms.
*
* If `debounceMode` is false (at end), schedule `callback` to
* execute after `delay` ms.
*/
timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
}
}
wrapper.cancel = cancel; // Return the wrapper function.
return wrapper;
}
/* eslint-disable no-undefined */
/**
* Debounce execution of a function. Debouncing, unlike throttling,
* guarantees that a function is only executed a single time, either at the
* very beginning of a series of calls, or at the very end.
*
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
* after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
* (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the debounced-function is executed.
*
* @returns {Function} A new, debounced function.
*/
function debounce (delay, atBegin, callback) {
return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
}
exports.debounce = debounce;
exports.throttle = throttle;
//# sourceMappingURL=index.cjs.js.map
{"version":3,"file":"index.cjs.js","sources":["throttle.js","debounce.js"],"sourcesContent":["/* eslint-disable no-undefined,no-param-reassign,no-shadow */\n\n/**\n * Throttle execution of a function. Especially useful for rate limiting\n * execution of handlers on events like resize and scroll.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.\n * @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the\n * throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time\n * after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,\n * the internal counter is reset).\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,\n * to `callback` when the throttled-function is executed.\n * @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),\n * schedule `callback` to execute after `delay` ms.\n *\n * @returns {Function} A new, throttled, function.\n */\nexport default function(delay, noTrailing, callback, debounceMode) {\n\t/*\n\t * After wrapper has stopped being called, this timeout ensures that\n\t * `callback` is executed at the proper times in `throttle` and `end`\n\t * debounce modes.\n\t */\n\tlet timeoutID;\n\tlet cancelled = false;\n\n\t// Keep track of the last time `callback` was executed.\n\tlet lastExec = 0;\n\n\t// Function to clear existing timeout\n\tfunction clearExistingTimeout() {\n\t\tif (timeoutID) {\n\t\t\tclearTimeout(timeoutID);\n\t\t}\n\t}\n\n\t// Function to cancel next exec\n\tfunction cancel() {\n\t\tclearExistingTimeout();\n\t\tcancelled = true;\n\t}\n\n\t// `noTrailing` defaults to falsy.\n\tif (typeof noTrailing !== 'boolean') {\n\t\tdebounceMode = callback;\n\t\tcallback = noTrailing;\n\t\tnoTrailing = undefined;\n\t}\n\n\t/*\n\t * The `wrapper` function encapsulates all of the throttling / debouncing\n\t * functionality and when executed will limit the rate at which `callback`\n\t * is executed.\n\t */\n\tfunction wrapper(...arguments_) {\n\t\tlet self = this;\n\t\tlet elapsed = Date.now() - lastExec;\n\n\t\tif (cancelled) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Execute `callback` and update the `lastExec` timestamp.\n\t\tfunction exec() {\n\t\t\tlastExec = Date.now();\n\t\t\tcallback.apply(self, arguments_);\n\t\t}\n\n\t\t/*\n\t\t * If `debounceMode` is true (at begin) this is used to clear the flag\n\t\t * to allow future `callback` executions.\n\t\t */\n\t\tfunction clear() {\n\t\t\ttimeoutID = undefined;\n\t\t}\n\n\t\tif (debounceMode && !timeoutID) {\n\t\t\t/*\n\t\t\t * Since `wrapper` is being called for the first time and\n\t\t\t * `debounceMode` is true (at begin), execute `callback`.\n\t\t\t */\n\t\t\texec();\n\t\t}\n\n\t\tclearExistingTimeout();\n\n\t\tif (debounceMode === undefined && elapsed > delay) {\n\t\t\t/*\n\t\t\t * In throttle mode, if `delay` time has been exceeded, execute\n\t\t\t * `callback`.\n\t\t\t */\n\t\t\texec();\n\t\t} else if (noTrailing !== true) {\n\t\t\t/*\n\t\t\t * In trailing throttle mode, since `delay` time has not been\n\t\t\t * exceeded, schedule `callback` to execute `delay` ms after most\n\t\t\t * recent execution.\n\t\t\t *\n\t\t\t * If `debounceMode` is true (at begin), schedule `clear` to execute\n\t\t\t * after `delay` ms.\n\t\t\t *\n\t\t\t * If `debounceMode` is false (at end), schedule `callback` to\n\t\t\t * execute after `delay` ms.\n\t\t\t */\n\t\t\ttimeoutID = setTimeout(\n\t\t\t\tdebounceMode ? clear : exec,\n\t\t\t\tdebounceMode === undefined ? delay - elapsed : delay\n\t\t\t);\n\t\t}\n\t}\n\n\twrapper.cancel = cancel;\n\n\t// Return the wrapper function.\n\treturn wrapper;\n}\n","/* eslint-disable no-undefined */\n\nimport throttle from './throttle';\n\n/**\n * Debounce execution of a function. Debouncing, unlike throttling,\n * guarantees that a function is only executed a single time, either at the\n * very beginning of a series of calls, or at the very end.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.\n * @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds\n * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.\n * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,\n * to `callback` when the debounced-function is executed.\n *\n * @returns {Function} A new, debounced function.\n */\nexport default function(delay, atBegin, callback) {\n\treturn callback === undefined\n\t\t? throttle(delay, atBegin, false)\n\t\t: throttle(delay, callback, atBegin !== false);\n}\n"],"names":["delay","noTrailing","callback","debounceMode","timeoutID","cancelled","lastExec","clearExistingTimeout","clearTimeout","cancel","undefined","wrapper","arguments_","self","elapsed","Date","now","exec","apply","clear","setTimeout","atBegin","throttle"],"mappings":";;;;AAAA;;AAEA;;;;;;;;;;;;;;;;AAgBe,mBAASA,KAAT,EAAgBC,UAAhB,EAA4BC,QAA5B,EAAsCC,YAAtC,EAAoD;AAClE;;;;;AAKA,MAAIC,SAAJ;AACA,MAAIC,SAAS,GAAG,KAAhB,CAPkE;;AAUlE,MAAIC,QAAQ,GAAG,CAAf,CAVkE;;AAalE,WAASC,oBAAT,GAAgC;AAC/B,QAAIH,SAAJ,EAAe;AACdI,MAAAA,YAAY,CAACJ,SAAD,CAAZ;AACA;AACD,GAjBiE;;;AAoBlE,WAASK,MAAT,GAAkB;AACjBF,IAAAA,oBAAoB;AACpBF,IAAA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AgBqB,OAAhB,EAAyBnB,QAAzB,EAAmC;AACjD,SAAOA,QAAQ,KAAKQ,SAAb,GACJY,QAAQ,CAACtB,KAAD,EAAQqB,OAAR,EAAiB,KAAjB,CADJ,GAEJC,QAAQ,CAACtB,KAAD,EAAQE,QAAR,EAAkBmB,OAAO,KAAK,KAA9B,CAFX;AAGA;;;;;"}
\ No newline at end of file
/* eslint-disable no-undefined,no-param-reassign,no-shadow */
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
*
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
* throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
* after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
* the internal counter is reset).
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the throttled-function is executed.
* @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
* schedule `callback` to execute after `delay` ms.
*
* @returns {Function} A new, throttled, function.
*/
function throttle (delay, noTrailing, callback, debounceMode) {
/*
* After wrapper has stopped being called, this timeout ensures that
* `callback` is executed at the proper times in `throttle` and `end`
* debounce modes.
*/
var timeoutID;
var cancelled = false; // Keep track of the last time `callback` was executed.
var lastExec = 0; // Function to clear existing timeout
function clearExistingTimeout() {
if (timeoutID) {
clearTimeout(timeoutID);
}
} // Function to cancel next exec
function cancel() {
clearExistingTimeout();
cancelled = true;
} // `noTrailing` defaults to falsy.
if (typeof noTrailing !== 'boolean') {
debounceMode = callback;
callback = noTrailing;
noTrailing = undefined;
}
/*
* The `wrapper` function encapsulates all of the throttling / debouncing
* functionality and when executed will limit the rate at which `callback`
* is executed.
*/
function wrapper() {
for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
arguments_[_key] = arguments[_key];
}
var self = this;
var elapsed = Date.now() - lastExec;
if (cancelled) {
return;
} // Execute `callback` and update the `lastExec` timestamp.
function exec() {
lastExec = Date.now();
callback.apply(self, arguments_);
}
/*
* If `debounceMode` is true (at begin) this is used to clear the flag
* to allow future `callback` executions.
*/
function clear() {
timeoutID = undefined;
}
if (debounceMode && !timeoutID) {
/*
* Since `wrapper` is being called for the first time and
* `debounceMode` is true (at begin), execute `callback`.
*/
exec();
}
clearExistingTimeout();
if (debounceMode === undefined && elapsed > delay) {
/*
* In throttle mode, if `delay` time has been exceeded, execute
* `callback`.
*/
exec();
} else if (noTrailing !== true) {
/*
* In trailing throttle mode, since `delay` time has not been
* exceeded, schedule `callback` to execute `delay` ms after most
* recent execution.
*
* If `debounceMode` is true (at begin), schedule `clear` to execute
* after `delay` ms.
*
* If `debounceMode` is false (at end), schedule `callback` to
* execute after `delay` ms.
*/
timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
}
}
wrapper.cancel = cancel; // Return the wrapper function.
return wrapper;
}
/* eslint-disable no-undefined */
/**
* Debounce execution of a function. Debouncing, unlike throttling,
* guarantees that a function is only executed a single time, either at the
* very beginning of a series of calls, or at the very end.
*
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
* after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
* (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the debounced-function is executed.
*
* @returns {Function} A new, debounced function.
*/
function debounce (delay, atBegin, callback) {
return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
}
export { debounce, throttle };
//# sourceMappingURL=index.esm.js.map
{"version":3,"file":"index.esm.js","sources":["throttle.js","debounce.js"],"sourcesContent":["/* eslint-disable no-undefined,no-param-reassign,no-shadow */\n\n/**\n * Throttle execution of a function. Especially useful for rate limiting\n * execution of handlers on events like resize and scroll.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.\n * @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the\n * throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time\n * after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,\n * the internal counter is reset).\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,\n * to `callback` when the throttled-function is executed.\n * @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),\n * schedule `callback` to execute after `delay` ms.\n *\n * @returns {Function} A new, throttled, function.\n */\nexport default function(delay, noTrailing, callback, debounceMode) {\n\t/*\n\t * After wrapper has stopped being called, this timeout ensures that\n\t * `callback` is executed at the proper times in `throttle` and `end`\n\t * debounce modes.\n\t */\n\tlet timeoutID;\n\tlet cancelled = false;\n\n\t// Keep track of the last time `callback` was executed.\n\tlet lastExec = 0;\n\n\t// Function to clear existing timeout\n\tfunction clearExistingTimeout() {\n\t\tif (timeoutID) {\n\t\t\tclearTimeout(timeoutID);\n\t\t}\n\t}\n\n\t// Function to cancel next exec\n\tfunction cancel() {\n\t\tclearExistingTimeout();\n\t\tcancelled = true;\n\t}\n\n\t// `noTrailing` defaults to falsy.\n\tif (typeof noTrailing !== 'boolean') {\n\t\tdebounceMode = callback;\n\t\tcallback = noTrailing;\n\t\tnoTrailing = undefined;\n\t}\n\n\t/*\n\t * The `wrapper` function encapsulates all of the throttling / debouncing\n\t * functionality and when executed will limit the rate at which `callback`\n\t * is executed.\n\t */\n\tfunction wrapper(...arguments_) {\n\t\tlet self = this;\n\t\tlet elapsed = Date.now() - lastExec;\n\n\t\tif (cancelled) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Execute `callback` and update the `lastExec` timestamp.\n\t\tfunction exec() {\n\t\t\tlastExec = Date.now();\n\t\t\tcallback.apply(self, arguments_);\n\t\t}\n\n\t\t/*\n\t\t * If `debounceMode` is true (at begin) this is used to clear the flag\n\t\t * to allow future `callback` executions.\n\t\t */\n\t\tfunction clear() {\n\t\t\ttimeoutID = undefined;\n\t\t}\n\n\t\tif (debounceMode && !timeoutID) {\n\t\t\t/*\n\t\t\t * Since `wrapper` is being called for the first time and\n\t\t\t * `debounceMode` is true (at begin), execute `callback`.\n\t\t\t */\n\t\t\texec();\n\t\t}\n\n\t\tclearExistingTimeout();\n\n\t\tif (debounceMode === undefined && elapsed > delay) {\n\t\t\t/*\n\t\t\t * In throttle mode, if `delay` time has been exceeded, execute\n\t\t\t * `callback`.\n\t\t\t */\n\t\t\texec();\n\t\t} else if (noTrailing !== true) {\n\t\t\t/*\n\t\t\t * In trailing throttle mode, since `delay` time has not been\n\t\t\t * exceeded, schedule `callback` to execute `delay` ms after most\n\t\t\t * recent execution.\n\t\t\t *\n\t\t\t * If `debounceMode` is true (at begin), schedule `clear` to execute\n\t\t\t * after `delay` ms.\n\t\t\t *\n\t\t\t * If `debounceMode` is false (at end), schedule `callback` to\n\t\t\t * execute after `delay` ms.\n\t\t\t */\n\t\t\ttimeoutID = setTimeout(\n\t\t\t\tdebounceMode ? clear : exec,\n\t\t\t\tdebounceMode === undefined ? delay - elapsed : delay\n\t\t\t);\n\t\t}\n\t}\n\n\twrapper.cancel = cancel;\n\n\t// Return the wrapper function.\n\treturn wrapper;\n}\n","/* eslint-disable no-undefined */\n\nimport throttle from './throttle';\n\n/**\n * Debounce execution of a function. Debouncing, unlike throttling,\n * guarantees that a function is only executed a single time, either at the\n * very beginning of a series of calls, or at the very end.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.\n * @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds\n * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.\n * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,\n * to `callback` when the debounced-function is executed.\n *\n * @returns {Function} A new, debounced function.\n */\nexport default function(delay, atBegin, callback) {\n\treturn callback === undefined\n\t\t? throttle(delay, atBegin, false)\n\t\t: throttle(delay, callback, atBegin !== false);\n}\n"],"names":["delay","noTrailing","callback","debounceMode","timeoutID","cancelled","lastExec","clearExistingTimeout","clearTimeout","cancel","undefined","wrapper","arguments_","self","elapsed","Date","now","exec","apply","clear","setTimeout","atBegin","throttle"],"mappings":"AAAA;;AAEA;;;;;;;;;;;;;;;;AAgBe,mBAASA,KAAT,EAAgBC,UAAhB,EAA4BC,QAA5B,EAAsCC,YAAtC,EAAoD;AAClE;;;;;AAKA,MAAIC,SAAJ;AACA,MAAIC,SAAS,GAAG,KAAhB,CAPkE;;AAUlE,MAAIC,QAAQ,GAAG,CAAf,CAVkE;;AAalE,WAASC,oBAAT,GAAgC;AAC/B,QAAIH,SAAJ,EAAe;AACdI,MAAAA,YAAY,CAACJ,SAAD,CAAZ;AACA;AACD,GAjBiE;;;AAoBlE,WAASK,MAAT,GAAkB;AACjBF,IAAAA,oBAAoB;AACpBF,IAAAA,SAAS,GAAG,IAAZ;AACA,GAvBiE;;;AA0BlE,MAAI,OAAOJ,UAAP,KAAsB,SAA1B,EAAqC;AACpCE,IAAAA,YAAY,GAAGD,QAAf;AACAA,IAAAA,QAAQ,GAAGD,UAAX;AACAA,IAAAA,UAAU,GAAGS,SAAb;AACA;AAED;;;;;;;AAKA,WAASC,OAAT,GAAgC;AAAA,sCAAZC,UAAY;AAAZA,MAAAA,UAAY;AAAA;;AAC/B,QAAIC,IAAI,GAAG,IAAX;AACA,QAAIC,OAAO,GAAGC,IAAI,CAACC,GAAL,KAAaV,QAA3B;;AAEA,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OINtV5Q3aSPoRg7o4+fA==",
"_location": "/throttle-debounce",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "throttle-debounce",
"name": "throttle-debounce",
"escapedName": "throttle-debounce",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/throttle-debounce/-/throttle-debounce-2.2.1.tgz",
"_shasum": "fbd933ae6793448816f7d5b3cae259d464c98137",
"_spec": "throttle-debounce",
"_where": "/Users/jiangjiantao/Documents/workspace/qingting/front/doublescreen-front",
"author": {
"name": "Ivan Nikolić",
"email": "niksy5@gmail.com",
"url": "http://ivannikolic.com"
},
"bugs": {
"url": "https://github.com/niksy/throttle-debounce/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Ben Alman",
"url": "http://benalman.com"
}
],
"dependencies": {},
"deprecated": false,
"description": "Throttle and debounce functions.",
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-transform-object-assign": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/runtime": "^7.2.0",
"babel-loader": "^8.0.4",
"babel-preset-niksy": "^4.1.0",
"changelog-verify": "^1.1.2",
"core-js": "^2.6.5",
"eslint": "^6.7.2",
"eslint-config-niksy": "^8.0.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-extend": "^0.1.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsdoc": "^18.4.3",
"eslint-plugin-mocha": "^6.2.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react": "^7.9.1",
"eslint-plugin-unicorn": "^14.0.1",
"esm": "^3.0.51",
"get-port": "^4.0.0",
"get-port-cli": "^2.0.0",
"github-release-from-changelog": "^2.1.1",
"husky": "^3.1.0",
"karma": "^4.0.1",
"karma-browserstack-launcher": "^1.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^0.1.7",
"karma-mocha-reporter": "^2.2.5",
"karma-qunit": "^0.1.9",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^3.0.0",
"lint-staged": "^9.5.0",
"minimist": "^1.2.0",
"np": "^3.0.4",
"prettier": "^1.17.0",
"qunitjs": "^1.23.1",
"rollup": "^1.0.0",
"rollup-plugin-babel": "^4.2.0",
"version-changelog": "^3.1.1",
"webpack": "^4.12.0"
},
"directories": {
"test": "test"
},
"engines": {
"node": ">=8"
},
"files": [
"index.cjs.{js,js.map}",
"index.esm.{js,js.map}",
"CHANGELOG.md",
"LICENSE.md",
"README.md"
],
"homepage": "https://github.com/niksy/throttle-debounce#readme",
"keywords": [
"debounce",
"throttle"
],
"license": "MIT",
"main": "index.cjs.js",
"module": "index.esm.js",
"name": "throttle-debounce",
"repository": {
"type": "git",
"url": "git+https://github.com/niksy/throttle-debounce.git"
},
"scripts": {
"build": "rollup --config rollup.config.js",
"lint": "eslint '{index,debounce,throttle,test/**/*}.js'",
"postpublish": "GITHUB_TOKEN=$GITHUB_RELEASE_TOKEN echo 'github-release-from-changelog'",
"prepublishOnly": "npm run build",
"release": "np",
"test": "npm run lint && npm run test:automated",
"test:automated": "BABEL_ENV=test SERVICE_PORT=$(get-port) karma start",
"test:automated:watch": "npm run test:automated -- --auto-watch --no-single-run",
"version": "version-changelog CHANGELOG.md && changelog-verify CHANGELOG.md && git add CHANGELOG.md"
},
"sideEffects": false,
"version": "2.2.1"
}
......@@ -7,6 +7,11 @@
"resolved": "https://registry.npmjs.org/mini-antui/-/mini-antui-0.4.34.tgz",
"integrity": "sha512-ZiJJwUa8kso2OQqBtJsRhliueXa7L8a++sAAZeNCnJ8nQ+eWfONV/8uV3IDdMEJEZKVZZTYsrFZcXRIwRXLomQ=="
},
"throttle-debounce": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/throttle-debounce/-/throttle-debounce-2.2.1.tgz",
"integrity": "sha512-i9hAVld1f+woAiyNGqWelpDD5W1tpMroL3NofTz9xzwq6acWBlO2dC8k5EFSZepU6oOINtV5Q3aSPoRg7o4+fA=="
},
"undefined": {
"version": "0.1.0",
"resolved": "https://registry.npm.taobao.org/undefined/download/undefined-0.1.0.tgz",
......
var api = require("../../utils/api.js");
var payUtils = require("../../utils/payUtils.js");
import { throttle } from 'throttle-debounce';
import { ACTION_GO_SCAN, ACTION_GO_HOME } from '../../const/actioncmd'
import { ACTION_ON_PAY_SUCCESS } from '../../const/actioncmd'
import { ACTION_ON_PAY_FAILED } from '../../const/actioncmd'
......@@ -150,7 +151,7 @@ Page({
/**
* 支付
*/
startPay() {
startPay:throttle(2000, function(e){
//开始loading
if (this.data.isPaying == true) {
......@@ -185,7 +186,7 @@ Page({
}
}
}.bind(this));
},
}),
/**
* 打开收银台,进行扫码收款
......
var api = require("../../utils/api.js");
import { throttle } from 'throttle-debounce';
import { Page } from '../../utils/scan';
//API
import { API_USE_COUPON } from '../../const/apiurl'
......
import { throttle } from 'throttle-debounce';
import { ACTION_LOGIN_OK } from '../../const/actioncmd'
Page({
data: {
......@@ -132,7 +133,7 @@ Page({
});
},
login() {
login:throttle(2000, function(){
if (this.data.loginDisabled) {
my.showToast({
......@@ -165,7 +166,7 @@ Page({
password: this.data.password,
callback: loginCallbackTemp
});
},
}),
loginCallback(res) {
console.log("业务层登录 success==>" + JSON.stringify(res));
if (res.code == 0) {
......
......@@ -27,7 +27,7 @@ export default class scan {
//兼容扫描枪
cur.onKeyPress.call(cur, code);
isScaning = false
}, 1200);
}, 2000);
} else {
//特殊处理 keycode需要减7
if (r.keyCode >= 7 && r.keyCode <= 16) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment