代码如下:
var getFunCallTimes = (function() {
// 装饰器,在当前函数执行前先执行另一个函数
function decoratorBefore(fn, beforeFn) {
return function() {
var ret = beforeFn.apply(this, arguments)
// 在前一个函数中判断,不需要执行当前函数
if (ret !== false) fn.apply(this, arguments)
}
}
// 执行次数
var funTimes = {}
// 给fun添加装饰器,fun执行前将进行计数累加
return function(fun, funName) {
// 存储的key值
funName = funName || fun
// 不重复绑定,有则返回
if (funTimes[funName]) return funTimes[funName]
// 绑定
funTimes[funName] = decoratorBefore(fun, function() {
// 计数累加
funTimes[funName].callTimes++
console.log('count', funTimes[funName].callTimes)
})
// 定义函数的值为计数值(初始化)
funTimes[funName].callTimes = 0
return funTimes[funName]
}
})()
function someFunction() {
}
function otherFunction() {
}
someFunction = getFunCallTimes(someFunction, 'someFunction');
someFunction(); // count 1
someFunction(); // count 2
someFunction(); // count 3
someFunction(); // count 4
console.log(someFunction.callTimes); // 4
otherFunction = getFunCallTimes(otherFunction);
otherFunction(); // count 1
console.log(otherFunction.callTimes); // 1
otherFunction(); // count 2
console.log(otherFunction.callTimes); // 2
console.log报Uncaught TypeError: Assignment to constant varia
开发移动端网页如何调用控制台(Eruda、vconsole)
上面是“JS如何实现console执行次数”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_2144.html
workflows工作流
一个浑身皮毛看起来像丘巴卡的3d动漫人物
一只白色的孔雀ComfyUI工作流
一个女孩骑着一辆生锈的现代摩托车
沙漠里美丽的图阿雷格女孩ComfyUI工作流
斗鸡场威武雄鸡ComfyUI工作流
图片转视频ComfyUI工作流
一只蚂蚁在花丛中找食物ComfyUI工作流
汉堡里的一只毛茸茸的小猫ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

利用js+css3做一个小鱼游泳特效
2023年程序猿如何给自己开启一场烟花盛会
利用CSS3代码编写45款按钮效果
利用css3做一个动态loading效果
barcode条形码/qrcode二维码兼容所有浏览器(含ie6/ie7/ie8)
js实现下雪特效
利用canvas画几个好玩的星云物种
jquery做一个漂亮挂墙动态时钟












