Module: utils/src/modules/__indexFunctions
Functions
after
▸ after(n
, func
): (this
: unknown
, ...args
: unknown
[]) => any
返回一个函数,该函数被调用n
或更多次之后将马上触发给定函数,调用时可传递参数,before
的反函数。
Example
const fn = after(2, (info) => {
console.log(info)
})
fn('params...')
=> undefined
fn('params...')
=> log 'params...'
const fn = after(2, (info) => {
console.log(info)
})
fn('params...')
=> undefined
fn('params...')
=> log 'params...'
Parameters
Name | Type | Description |
---|---|---|
n | number | 调用次数 |
func | Func <any > | 给定函数 |
Returns
fn
▸ (this
, ...args
): any
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
any
before
▸ before(n
, func
): (this
: unknown
, ...args
: unknown
[]) => unknown
返回一个函数,该函数将调用给定函数次数不超过n
次,调用时可传递参数, 之后再调用这个函数,将返回一次最后调用给定函数的结果。
Example
const fn = before(2, (val) => val + 1)
fn(1)
=> 2
fn(2)
=> 2
fn(3)
=> 2
const fn = before(2, (val) => val + 1)
fn(1)
=> 2
fn(2)
=> 2
fn(3)
=> 2
Parameters
Name | Type | Description |
---|---|---|
n | number | 调用次数 |
func | Func <any > | 给定函数 |
Returns
fn
▸ (this
, ...args
): unknown
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
unknown
compose
▸ compose(...functions
): (this
: unknown
, ...args
: unknown
[]) => unknown
返回一个由给定函数数组组合后的复合函数,也就是一个函数执行完之后把返回的结果再作为参数赋值给下一个函数来执行。以此类推,在数学里,把函数f(), g(), 和 h()
组合起来可以得到复合函数f(g(h()))
。
Example
const fn = compose((val) => val += 1, (val) => val += 2)
fn(1)
=> 4
const fn = compose((val) => val += 1, (val) => val += 2)
fn(1)
=> 4
Parameters
Name | Type | Description |
---|---|---|
...functions | Func <any >[] | 给定函数数组 |
Returns
fn
▸ (this
, ...args
): unknown
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
unknown
debounce
▸ debounce(func
, wait
, options?
): (this
: unknown
, ...args
: unknown
[]) => unknown
返回一个防抖函数,该函数会从上一次被调用后,延迟wait
毫秒后调用给定函数。 给定函数调用时会传入最后一次提供给debounced
函数的参数。 后续调用的debounced
函数返回是最后一次给定函数调用的结果。 如果wait
为0
并且leading
为false
,给定函数调用将被推迟到下一个点,类似setTimeout
为0
的超时。
Method
debounce.cancel()
取消防抖函数
debounce.flush()
立即调用防抖函数
debounce.pending()
判断是否有正在执行的防抖函数
Example
// 避免窗口在变动时出现昂贵的计算开销
jQuery(window).on('resize', debounce(calculateLayout, 150))
// 当点击时 `sendMail` 随后就被调用
jQuery(element).on('click', debounce(sendMail, 300, {
'leading': true,
'trailing': false
}))
// 确保 `batchLog` 调用1次之后,1秒内会被触发
const debounced = debounce(batchLog, 250, { 'maxWait': 1000 })
const source = new EventSource('/stream')
jQuery(source).on('message', debounced)
// 取消一个 trailing 的防抖动调用
jQuery(window).on('popstate', debounced.cancel)
// 避免窗口在变动时出现昂贵的计算开销
jQuery(window).on('resize', debounce(calculateLayout, 150))
// 当点击时 `sendMail` 随后就被调用
jQuery(element).on('click', debounce(sendMail, 300, {
'leading': true,
'trailing': false
}))
// 确保 `batchLog` 调用1次之后,1秒内会被触发
const debounced = debounce(batchLog, 250, { 'maxWait': 1000 })
const source = new EventSource('/stream')
jQuery(source).on('message', debounced)
// 取消一个 trailing 的防抖动调用
jQuery(window).on('popstate', debounced.cancel)
Parameters
Name | Type | Description |
---|---|---|
func | Func <any > | 需防抖动的函数 |
wait | number | 需要延迟的毫秒数 |
options? | DebounceOption | 给定选项对象 |
Returns
fn
▸ (this
, ...args
): unknown
防抖函数
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
unknown
Name | Type |
---|---|
cancel | () => void |
flush | () => unknown |
pending | () => boolean |
delay
▸ delay(func
, wait
, ...args
): Promise
<unknown
>
返回一个promise
对象,类似setTimeout
,等待wait
毫秒后调用给定函数。如果传递可选的参数args
,当给定函数执行时,args
以数组的形式作为参数传入,可附加await
暂停函数等待执行。
Example
await delay(console.log, 1000, 'logged later')
=> 'logged later' // 一秒钟后执行
await delay(console.log, 1000, 'logged later')
=> 'logged later' // 一秒钟后执行
Parameters
Name | Type | Description |
---|---|---|
func | Func <any > | 给定函数 |
wait | number | 等待毫秒数 |
...args | unknown [] | 附加参数 |
Returns
Promise
<unknown
>
negate
▸ negate<F
>(predicate
): (this
: unknown
, ...args
: unknown
[]) => boolean
返回给定函数的否定版本函数。
Example
var isFalsy = negate(Boolean)
find([-2, -1, 0, 1, 2], isFalsy)
=> 0
var isFalsy = negate(Boolean)
find([-2, -1, 0, 1, 2], isFalsy)
=> 0
Type parameters
Name |
---|
F |
Parameters
Name | Type | Description |
---|---|---|
predicate | F | 给定函数 |
Returns
fn
▸ (this
, ...args
): boolean
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
boolean
once
▸ once(func
): (this
: unknown
, ...args
: unknown
[]) => unknown
返回一个函数,此函数只能调用给定函数一次,重复调用返回第一次调用的结果,调用给定函数时可传递参数。
Example
const initialize = once(createApplication)
initialize()
initialize() // `initialize` 只能调用 `createApplication` 一次。
const initialize = once(createApplication)
initialize()
initialize() // `initialize` 只能调用 `createApplication` 一次。
Parameters
Name | Type | Description |
---|---|---|
func | Func <any > | 给定函数 |
Returns
fn
▸ (this
, ...args
): unknown
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
unknown
throttle
▸ throttle(func
, wait
, options?
): (this
: unknown
, ...args
: unknown
[]) => unknown
创建一个节流函数,在给定毫秒值内最多执行给定函数一次。 给定函数调用时会传入最后一次提供给throttle
函数的参数。 后续调用的throttle
函数返回的是最后一次给定函数调用的结果。 如果给定毫秒值为0
并且leading
为false
,给定函数调用将被推迟到下一个点,类似setTimeout
为0
的超时。
Method
cancel()
取消节流函数
flush()
立即调用节流函数
pending()
判断是否有正在执行的节流函数
Example
// 避免在滚动时过分的更新定位
jQuery(window).on('scroll', throttle(updatePosition, 100));
// 点击后就调用 `renewToken`,但5分钟内超过1次。
var throttled = throttle(renewToken, 300000, { 'trailing': false });
jQuery(element).on('click', throttled);
// 取消一个 trailing 的节流调用。
jQuery(window).on('popstate', throttled.cancel);
// 避免在滚动时过分的更新定位
jQuery(window).on('scroll', throttle(updatePosition, 100));
// 点击后就调用 `renewToken`,但5分钟内超过1次。
var throttled = throttle(renewToken, 300000, { 'trailing': false });
jQuery(element).on('click', throttled);
// 取消一个 trailing 的节流调用。
jQuery(window).on('popstate', throttled.cancel);
Parameters
Name | Type | Description |
---|---|---|
func | Func <any > | 给定函数 |
wait | number | 给定毫秒值 |
options? | Omit <DebounceOption , "maxWait" > | 给定选项对象 |
Returns
fn
▸ (this
, ...args
): unknown
防抖函数
Parameters
Name | Type |
---|---|
this | unknown |
...args | unknown [] |
Returns
unknown
Name | Type |
---|---|
cancel | () => void |
flush | () => unknown |
pending | () => boolean |