-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathevent.js
52 lines (48 loc) · 957 Bytes
/
event.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
const eventName = {};
const down = (o) => { // 键盘、手指按下
const keys = Object.keys(eventName);
keys.forEach(i => {
clearTimeout(eventName[i]);
eventName[i] = null;
});
if (!o.callback) {
return;
}
const clear = () => {
clearTimeout(eventName[o.key]);
};
o.callback(clear);
if (o.once === true) {
return;
}
let begin = o.begin || 100;
const interval = o.interval || 50;
const loop = () => {
eventName[o.key] = setTimeout(() => {
begin = null;
loop();
o.callback(clear);
}, begin || interval);
};
loop();
};
const up = (o) => { // 键盘、手指松开
clearTimeout(eventName[o.key]);
eventName[o.key] = null;
if (!o.callback) {
return;
}
o.callback();
};
const clearAll = () => {
const keys = Object.keys(eventName);
keys.forEach(i => {
clearTimeout(eventName[i]);
eventName[i] = null;
});
};
export default {
down,
up,
clearAll,
};