-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelayed.js
More file actions
76 lines (64 loc) · 1.81 KB
/
delayed.js
File metadata and controls
76 lines (64 loc) · 1.81 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
/*****************************************************************
* Delayed: A collection of setTimeout-related function wranglers
* Copyright (c) Rod Vagg (@rvagg) 2014
* https://github.com/rvagg/delayed
* License: MIT
*/
;(function init (name, definition) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = definition()
} else {
this[name] = definition()
}
}('delayed', function setup () {
const context = this
const old = context.delayed
const deferMs = 1
function slice (arr, i) {
return Array.prototype.slice.call(arr, i)
}
function delay (fn, ms, ctx) {
var args = slice(arguments, 3)
return setTimeout(function delayer () {
fn.apply(ctx || null, args)
}, ms)
}
function defer (fn, ctx) {
return delay.apply(null, [ fn, deferMs, ctx ].concat(slice(arguments, 2)))
}
function delayed () {
var args = slice(arguments)
return function delayeder () {
return delay.apply(null, args.concat(slice(arguments)))
}
}
function deferred (fn, ctx) {
return delayed.apply(null, [ fn, deferMs, ctx ].concat(slice(arguments, 2)))
}
function cumulativeDelayed (fn, ms, ctx) {
var args = slice(arguments, 3)
var timeout = null
return function cumulativeDelayeder () {
var _args = slice(arguments)
var f = function cumulativeDelayedCaller () {
return fn.apply(ctx || null, args.concat(_args))
}
if (timeout != null) { clearTimeout(timeout) }
timeout = setTimeout(f, ms)
return timeout
}
}
function noConflict () {
context.delayed = old
return this
}
return {
delay: delay,
defer: defer,
delayed: delayed,
deferred: deferred,
noConflict: noConflict,
cumulativeDelayed: cumulativeDelayed,
debounce: cumulativeDelayed
}
}))