-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgs-utils-func.js
More file actions
79 lines (63 loc) · 1.58 KB
/
Copy pathgs-utils-func.js
File metadata and controls
79 lines (63 loc) · 1.58 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use strict";
/* eslint-disable no-empty-function */
function GSUnoop() {}
function GSUnoopFalse() { return false; }
/* eslint-enable */
// .............................................................................
function GSUsetTimeout( fn, sec ) {
const ms = sec * 1000 | 0;
return ms > 0
? setTimeout( fn, ms )
: ( fn(), null );
}
let _GSUsetInterval_minMs = 0;
function GSUsetIntervalLimit( sec ) {
return _GSUsetInterval_minMs = sec * 1000 | 0;
}
function GSUsetInterval( fn, sec ) {
const ms = Math.max( _GSUsetInterval_minMs, sec * 1000 | 0 );
if ( ms ) {
return setInterval( fn, ms );
}
const obj = Object.seal( { $id: null } );
const fn2 = () => {
fn();
obj.$id = requestAnimationFrame( fn2 );
};
fn2();
return obj;
}
function GSUclearTimeout( id ) {
clearTimeout( id );
}
function GSUclearInterval( id ) {
GSUisObj( id )
? cancelAnimationFrame( id.$id )
: clearInterval( id );
}
// .............................................................................
function GSUwait( sec ) {
return new Promise( res => GSUsetTimeout( res, sec ) );
}
// .............................................................................
function GSUdebounce( fn, sec ) {
let timeoutId;
return ( ...args ) => {
GSUclearTimeout( timeoutId );
return timeoutId = GSUsetTimeout( () => fn( ...args ), sec );
};
}
function GSUthrottle( fn, sec ) {
let timeoutId;
let argsSaved;
return ( ...args ) => {
argsSaved = args;
if ( !timeoutId ) {
timeoutId = GSUsetTimeout( () => {
fn( ...argsSaved );
timeoutId = null;
}, sec );
}
return timeoutId;
};
}