1
This commit is contained in:
25
20220330_Vote/Ewide.Web.Entry/wwwroot/lib/xe-utils/once.js
Normal file
25
20220330_Vote/Ewide.Web.Entry/wwwroot/lib/xe-utils/once.js
Normal file
@@ -0,0 +1,25 @@
|
||||
var slice = require('./slice')
|
||||
|
||||
/**
|
||||
* 创建一个只能调用一次的函数,只会返回第一次执行后的结果
|
||||
*
|
||||
* @param {Function} callback 函数
|
||||
* @param {Object} context 上下文
|
||||
* @param {*} args 额外的参数
|
||||
* @return {Object}
|
||||
*/
|
||||
function once (callback, context) {
|
||||
var done = false
|
||||
var rest = null
|
||||
var args = slice(arguments, 2)
|
||||
return function () {
|
||||
if (done) {
|
||||
return rest
|
||||
}
|
||||
rest = callback.apply(context, slice(arguments).concat(args))
|
||||
done = true
|
||||
return rest
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = once
|
||||
Reference in New Issue
Block a user