-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (57 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @author Behnam Mohammadi
*/
'use strict';
exports.__esModule = true;
const getCurrentTime =
typeof performance === 'object' && typeof performance.now === 'function'
? () => performance.now()
: () => Date.now();
/**
* @description throttle
* @public
* @version 1.0.5
* @param {function} func
* @param {number} delay
* @returns {function} throttled function
*/
exports.throttle = function throttle(func, delay) {
var nextAllowed = 0;
return function () {
var now = getCurrentTime();
if (now < nextAllowed) return;
nextAllowed = now + delay;
func.apply(this, arguments);
};
};
/**
* @description debounce
* @public
* @version 1.0.5
* @param {function} func
* @param {number} delay
* @returns {function} debounced function
*/
exports.debounce = function debounce(func, delay) {
let timeoutId = null;
return function () {
clearTimeout(timeoutId);
var args = arguments;
var selfThis = this;
timeoutId = setTimeout(function () {
func.apply(selfThis, args);
}, delay);
};
};
/**
* @description delay
* @public
* @version 1.1.0
* @param {number} time
* @return {object} promise
*/
exports.delay = function (time) {
return new Promise(function (done) {
setTimeout(done, time);
});
};