-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.timer-tools.coffee
More file actions
70 lines (46 loc) · 1.54 KB
/
jquery.timer-tools.coffee
File metadata and controls
70 lines (46 loc) · 1.54 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
###!
jquery.timer-tools v0.1.0
by Andrey Mikhaylov (lolmaus) lolmaus@gmail.com
inspired by https://code.google.com/p/jquery-debounce/
MIT license
###
jQuery.extend
delay: (msTimeout, callback) ->
setTimeout callback, msTimeout
debounceLast: (msTimeout, context, callback) ->
if arguments.length < 2
throw "debounceLast called with less than two arguments"
else if arguments.length is 2
[context, callback] = [undefined, context]
timer = null
(args...) ->
context = context or this
clearTimeout timer
timer = jQuery.delay msTimeout, ->
callback.apply context, args
timer = null
debounceFirst: (msTimeout, context, callback) ->
if arguments.length < 2
throw "debounceFirst called with less than two arguments"
else if arguments.length is 2
[context, callback] = [undefined, context]
timer = null
(args...) ->
context = context or this
unless timer
callback.apply context, args
clearTimeout timer
timer = jQuery.delay msTimeout, ->
timer = null
throttle: (msTimeout, context, callback) ->
if arguments.length < 2
throw "throttle called with less than two arguments"
else if arguments.length is 2
[context, callback] = [undefined, context]
timer = null
(args...) ->
context = context or this
unless timer
callback.apply context, args
timer = jQuery.delay msTimeout, ->
timer = null