-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjquery.holdEvent.js
59 lines (52 loc) · 1.32 KB
/
jquery.holdEvent.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
(function($) {
function holdClearer(isTimeout) {
return function(event) {
isTimeout ?
clearTimeout(event.data)
: clearInterval(event.data);
}
}
function holdEventDelayedLoop(element, handler, time) {
element.on(
'mouseup mouseout',
setTimeout(
function() {
element.on(
'mouseup mouseout',
setInterval(function() { handler(element); }, time / 10),
holdClearer(false)
);
},
time
),
holdClearer(true)
);
}
$.fn.holdEvent = function(options) {
var settings = $.extend(
{
handler: function() {},
time: 500
},
options),
element = this;
this.mousedown(
function() {
return setTimeout(
function() {
settings.handler(element);
holdEventDelayedLoop(element, settings.handler, settings.time)
},
settings.time);
},
function(eventDown) {
$(eventDown.target).on(
'mouseup mouseout',
eventDown.data(),
holdClearer(true)
);
return false;
});
return this;
}
})(jQuery)